Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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?
- import sys, traceback, random
- def Game(switch):
- # players options
- doors = []
- doors.append("Car")
- doors.append("Goat")
- doors.append("Goat")
- # player chooses a random door
- players_choice = doors[random.randint(0,2)]
- doors.pop(random.randint(1,2))
- # switch or stick with initial choice
- if (switch):
- if (players_choice == "Car"):
- return False
- else:
- return True
- else:
- return players_choice == "Car"
- def Run():
- tries = int(raw_input("Number of tries: "))
- switch_success = 0
- stay_success = 0
- # play and switch
- for index in range(tries):
- if (Game(True)):
- switch_success += 1
- # play and keep original choice
- for index in range(tries):
- if (Game(False)):
- stay_success += 1
- switch_percent = (float(switch_success)/float(tries))*100
- stay_percent = (float(stay_success)/float(tries))*100
- print "Switching success: " + str(switch_percent)
- print "Staying success: " + str(stay_percent)
- def main(args):
- try:
- Run();
- except:
- traceback.print_exc(file=sys.stdout)
- # error handling code here
- return 1 # exit on error
- else:
- return 0 # exit errorlessly
- if __name__ == '__main__':
- sys.exit(main(sys.argv))
Add Comment
Please, Sign In to add comment