fork download
  1. import random, copy
  2.  
  3. def simulate_game():
  4. doors = range(3) #0, 1, 2
  5. car = random.choice(doors)
  6.  
  7. contestant_selection = random.choice(doors)
  8. doors.remove(contestant_selection)
  9.  
  10. monty_doors = copy.copy(doors)
  11. # Choices for Monty to open to reveal a goat
  12.  
  13. try:
  14. monty_doors.remove(car) # Hall won't reveal a car
  15. except ValueError:
  16. # If user picked the car to start and therefore is already removed
  17. pass
  18.  
  19. monty_goat_reveal = random.choice(monty_doors)
  20. doors.remove(monty_goat_reveal) # Remove Goat door
  21.  
  22. switched_choice = doors.pop()
  23.  
  24. return switched_choice == car
  25.  
  26. if __name__ == '__main__':
  27. counter = 0
  28. trials = 10000
  29. for x in xrange(trials):
  30. if simulate_game():
  31. counter += 1
  32. print 'Percentage won is: {}%'.format(100. * counter / trials)
  33.  
Success #stdin #stdout 0.09s 11496KB
stdin
Standard input is empty
stdout
Percentage won is: 67.97%