### Project 8: Verification of the Monte Hall Paradox

For this project you'll use simulation to prove that switching doors gives you the best chance of winning the car in the Monte Hall problem.

#### Exercises

Turn in exercises 32, 33, 34 and 35 with this assignment.

Exercise 36 is titled “Homework”. That exercise has important practice with loops. It is due with this assignment. Please give yourself extra time to do it.

### Setup

You will use your `project7.py`

file in this assignment and you will create a `project8.py`

as well. Start the `project8.py`

file with the following code:

import random import project7 def main() : car = random.randrange(1, 4) choice1 = random.randrange(1, 4) monty = project7.monty_door(car, choice1) choice2 = random.choice(['stay', 'switch']) if project7.has_won(car, choice1, choice2) : print ('You won the car!') else: print ('You won the goat!') if __name__ == '__main__' : main()

Test to be sure the code above works. Your `project7.py`

file must be in the same directory as your `project8.py`

file. Also, `project8.py`

assumes that you have coded your functions exactly as I asked and that you don't run any code outside of `main()`

. If you see errors, verify your functions. Running the code repeatedly should look like this:

$ python3.6 ./project8.py You won the car! $ python3.6 ./project8.py You won the goat! $ python3.6 ./project8.py You won the goat! $ python3.6 ./project8.py You won the goat! $ python3.6 ./project8.py You won the car!

Of course, whether you won the car or the goat will be random.

### Simulation

Your simulator will run the Monty Hall problem a number of times that's specified by the user. Your program should prompt the user for the number of times to run the simulation. **Warning: Your program must handle invalid input gracefully.** The grading program will test for this.

Your program should run like this:

$ python3.6 ./project8.py Enter the number of turns: 1000 Switching wins 68.66537717601547% of the time Staying wins 39.75155279503105% of the time

If the user enters a bogus number the program must reprompt the user:

$ python3.6 ./project8.py Enter the number of turns: bogus Enter the number of turns: 1000 Switching wins 66.86390532544378% of the time Staying wins 33.46855983772819% of the time

#### Hints

The code you are given generates a random game. Use a `for`

loop to loop the given number of times. Each time use `if`

statements to count the following quantities:

- The number of times the contestant switched doors
- The number of times the contestant won after switching
- The number of times the contestant stayed with the same door
- The number of times the contestant won after staying

When the loop is over the probabilities are:

- won_after_switch / switch_total
- won_after_stay / stay_total

Do you remember how to *increment* a counter variable? Here's how:

count = count + 1

### Requirements

- Your program must be called
`project8.py`

- You must also resubmit
`project7.py`

(you can fix it if you need to) - Taking input:
- Accept numbers between 0 and 100,000 (including 100,000)
- Your program must gracefully handle non-numbers.

- The output of your program must closely match mine:
- Output percentages on separate lines
- The “switch” percentage first

### Extra Credit (10 points)

Improve your simulator by generalizing the Monty Hall problem to any number of doors. In the general Monty Hall problem Monty opens every remaining door except for one. The contestant therefore sees two closed doors, the one they chose initially and the one that Monty left. Then they get to stay or switch.

Put your extra credit in a separate file called `project8_xc.py`

. You should not rewrite `project7.py`

and your extra credit assignment should **NOT** import `project7`

.

The extra credit assignment should take the number of doors to simulate on the command line:

$ python3 ./project8.py 10 Number of doors: 10 Enter the number of turns: 100 Switching wins 90.129% of the time Staying wins 9.871% of the time

Your general Monty Hall must have an updated `monty_door`

function:

`monty_door`

- Arguments:
**INTEGER**The number of the door with the car behind it**INTEGER**The number of the door that was guessed by the contestant**INTEGER**The total number of doors.

- Returns:
- A list of integers representing the doors that Monty opens.

The `has_won`

function must remain the same.

### Extra Credit Requirements

- Your
`monty_door`

function must take arguments as specified.- The list of doors starts at 1
- The list must have the total number of doors minus two

- The extra credit must operate just like the simulator
- Take a number of turns just like
`project8.py`

- The number of doors should be specified on the command line
- The code must be in the file
`project8_xc.py`

- You must do the first part of Project 8 to receive extra credit.

### Grading (25 points total, plus 10 extra credit)

- 5 points for the exercises
- 5 points for exercise 36
- 5 points for a correct submission
- 10 points for the correctness of the program
- 10 points extra credit for
`project8_xc.py`