# This program simulates the Monty Hall game show. # The setup is as follows: There are three closed # doors. Behind one of the doors is a car, and behind # the other two are goats. You pick one of the doors. # The game show host then opens one of the two doors # that you didn't pick. You then have the option # to switch your guess to the third door. Assume that # your goal is to win the car. In this program, we test # the proportion of times you win by choosing to switch # after one of the doors is revealed. import random def montyhall(): car = random.randint(0,2) guess = random.randint(0,2) if guess != car: opendoor = 3-car-guess else: while True: opendoor = random.randint(0,2) if opendoor != car: break switch = 3 - guess - opendoor return (switch == car) def propwins(n): count = 0. for _ in range(n): if montyhall(): count += 1 return count/n print propwins(100)