Guest User

Untitled

a guest
Oct 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. # Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1 [but the door is not opened], and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?
  2.  
  3. import sys, traceback, random
  4.  
  5. def Game(switch):
  6.  
  7.     # players options
  8.     doors = []
  9.     doors.append("Car")
  10.     doors.append("Goat")
  11.     doors.append("Goat")
  12.    
  13.     # player chooses a random door
  14.     players_choice = doors[random.randint(0,2)]
  15.    
  16.     doors.pop(random.randint(1,2))
  17.    
  18.     # switch or stick with initial choice
  19.     if (switch):        
  20.        
  21.         if (players_choice == "Car"):
  22.             return False
  23.         else:
  24.             return True
  25.              
  26.     else:
  27.         return players_choice == "Car"    
  28.        
  29. def Run():
  30.    
  31.     tries = int(raw_input("Number of tries: "))
  32.    
  33.     switch_success = 0
  34.     stay_success = 0
  35.      
  36.     # play and switch
  37.     for index in range(tries):
  38.         if (Game(True)):
  39.             switch_success += 1
  40.            
  41.     # play and keep original choice
  42.     for index in range(tries):
  43.         if (Game(False)):
  44.             stay_success += 1        
  45.            
  46.     switch_percent = (float(switch_success)/float(tries))*100
  47.     stay_percent = (float(stay_success)/float(tries))*100
  48.  
  49.     print "Switching success: " + str(switch_percent)
  50.     print "Staying success: " + str(stay_percent)
  51.  
  52. def main(args):
  53.     try:
  54.         Run();
  55.     except:
  56.         traceback.print_exc(file=sys.stdout)
  57.         # error handling code here
  58.         return 1  # exit on error
  59.     else:
  60.         return 0  # exit errorlessly
  61.  
  62. if __name__ == '__main__':
  63.     sys.exit(main(sys.argv))
Add Comment
Please, Sign In to add comment