In Conditionals, we designed the Monty Hall game. By using a list, we can simplify the code:
import random # define a list of doors doors = [1, 2, 3] # use random.randint() function to generate a random number between 1 and 3 # The number generated means the door that has the car behind it winner = random.randint(1,3) # ask the player to make the initial decision print 'Please choose the number of the door (enter 1,2, or 3):' player_choice = int(raw_input()) open_door = 0 # the number of the door will be opened switch_door = 0 # the number of the door if the player choose to switch ## remove the winner door from the list, as we will not open it until the very end doors.remove(winner) ## if the player chose the door with a goat, ## open the other door with the goat if player_choice != winner: switch_door = winner doors.remove(player_choice) open_door = doors ## if the player chose the door with the car, ## open one of the other two doors randomly else: open_choice = random.randint(0, 1) # randomly choose a door between 2 doors left in the list doors open_door = doors[open_choice] doors.remove(open_door) switch_door = doors print 'Door',open_door,'is opened and there is a goat behind it.' print 'Do you want to switch?' print 'Y - Yes' print 'N - No' want_switch = raw_input() if want_switch == 'Y': player_choice = switch_door #switch to the other door if player_choice == winner: print 'Congratulations! You win the car!' else: print 'You get a goat!'