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.


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.


You will use your file in this assignment and you will create a as well. Start the 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!')
        print ('You won the goat!')
if __name__ == '__main__' :

Test to be sure the code above works. Your file must be in the same directory as your file. Also, 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 ./ 
You won the car!
$ python3.6 ./ 
You won the goat!
$ python3.6 ./ 
You won the goat!
$ python3.6 ./ 
You won the goat!
$ python3.6 ./ 
You won the car!

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


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 ./ 
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 ./ 
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


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:

  1. The number of times the contestant switched doors
  2. The number of times the contestant won after switching
  3. The number of times the contestant stayed with the same door
  4. The number of times the contestant won after staying

When the loop is over the probabilities are:

  1. won_after_switch / switch_total
  2. won_after_stay / stay_total

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

count = count + 1 


  • Your program must be called
  • You must also resubmit (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 You should not rewrite 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 ./ 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:

  1. monty_door
    1. Arguments:
      1. INTEGER The number of the door with the car behind it
      2. INTEGER The number of the door that was guessed by the contestant
      3. INTEGER The total number of doors.
    2. Returns:
      1. A list of integers representing the doors that Monty opens.

The has_won function must remain the same.

Extra Credit Requirements

  1. Your monty_door function must take arguments as specified.
    1. The list of doors starts at 1
    2. The list must have the total number of doors minus two
  2. The extra credit must operate just like the simulator
    1. Take a number of turns just like
  3. The number of doors should be specified on the command line
  4. The code must be in the file
  5. 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