A skeleton program
import random
# 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 # records the number of the door will be opened
switch_door = 0 # the number of the door if the player choose to switch
## OPEN A DOOR WITH A GOAT
## if the player chose a door with a goat,
## open the other door with the goat
if player_choice != winner:
….
## if the player chose the door with the car,
## open one of the other two doors, each having a goat
## choose a random one (each with probability ½)
else:
…..
print 'Door', open_door, 'is opened and there is a goat behind it.'
print 'Do you want to switch?'
print 'Y for Yes, I want to switch'
print 'N for No, I don’t want to switch'
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 win a goat!'
A sample program
# -*- coding: utf-8 -*-
import random
# 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
## if the player chose the door with a goat,
## open the other door with the goat
if player_choice != winner:
switch_door = winner
if player_choice == 1:
if winner == 2:
open_door = 3
else:
open_door = 2
elif player_choice == 2:
if winner == 1:
open_door = 3
else:
open_door = 1
else: # choice is 3
if winner == 1:
open_door = 2
else:
open_door = 1
## if the player chose the door with the car,
## open one of the other two doors randomly
else:
if player_choice == 1:
goat_door1 = 2
goat_door2 = 3
elif player_choice == 2:
goat_door1 = 1
goat_door2 = 3
else: # choice is 3
goat_door1 = 1
goat_door2 = 2
# randomly open a door between two goat_doors
luck = random.randint(1,2)
if luck == 1:
open_door = goat_door1
switch_door = goat_door2
else:
open_door = goat_door2
switch_door = goat_door1
print 'Door', open_door, 'is opened and there is a goat behind it.'
print 'Do you want to switch?'
print 'Y for Yes, I want to switch'
print 'N for No, I don’t want to switch'
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 win a goat!'
