# A computer simulation of the Monty Hall problem, # a counter-intuitive logic and probability puzzle. # by @blogmywiki - www.suppertime.co.uk/blogmywiki print('Welcome to the\n' ' __ ___ __ __ __ ____\n' ' / |/ /___ ____ / /___ __ / / / /___ _/ / /\n' ' / /|_/ / __ \/ __ \/ __/ / / / / /_/ / __ `/ / / \n' ' / / / / /_/ / / / / /_/ /_/ / / __ / /_/ / / / \n' '/_/ /_/\____/_/ /_/\__/\__, / /_/ /_/\__,_/_/_/ \n' ' /____/ show\n' ' |-----------------------|\n' 'door no.| 0 | 1 | 2 |\n' ' |-----------------------|\n' ' | goat | goat | car |\n' ' |-----------------------|\n') while True: import random print() answer = input('How many games do you want to simulate? (q to quit) ') if answer == "q": break else: tries = int(answer) doors = ['goat','goat','car'] # door no. 0 1 2 # I don't think it matters that these are not random # and keeping this fixed makes coding easier. # Both players will pick the same door, but # when a door is opened with a goat behind it, # player1 will always stick with their 1st choice # whereas player2 will always change. # We will then keep a tally of who won more cars. player1_tally = 0 player2_tally = 0 for i in range(tries): chosen_door = random.randint(0,2) # pick a random door number between 0 and 2 print('Game',i) print ('door',chosen_door,'chosen, containing a',doors[chosen_door]) player1_choice = chosen_door if chosen_door == 0: player2_choice = 2 print('Host opens door 1, player2 switches choice to door 2') elif chosen_door == 1: player2_choice = 2 print('Host opens door 0, player2 switches choice to door 2') elif chosen_door == 2: player2_choice = 1 print('Host opens door 0, player2 switches choice to door 1') if doors[player1_choice] == "car": print('Player 1 won a car!') player1_tally = player1_tally + 1 if doors[player2_choice] == "car": print('Player 2 won a car!') player2_tally = player2_tally + 1 print() print('Player 1 who stuck won',player1_tally,'cars.') print('Player 2 who changed won',player2_tally,'cars.')