All my code is below.
Just started learning to program this week. I'm attempting to make a Monty Hall simulator (text only) where the player chooses a door by selecting 1, 2 or 3. For some reason, though, Python doesn't seem to be recognizing the input!
Here are links to the game for the uninitiated:
What my program attempts to do is as follows. First the player chooses a door, either 1, 2 or 3. Then the program checks to make sure that the player did indeed enter one of those three numbers. If not, then the person needs to choose again.
After this, the game randomly chooses a winning door. Then, per the rules of the game, the program needs to reveal a dummy prize (the goat). So the program randomly chooses one of the doors to be the "goat door." The program first makes sure that this door is not the winning door nor is it the chosen door.
Here's the error I get when running my code:
line 52, in <module>
doors()
line 14, in doors
while goatDoor == chosenDoor or goatDoor == winningDoor:
NameError: name 'chosenDoor' is not defined
My issue is that I can't tell why it keeps saying that chosenDoor is not defined!
Here's the code:
import random
def chooseDoor(): # choose a door
chosenDoor = ''
while chosenDoor != 1 and chosenDoor != 2 and chosenDoor != 3:
print('Choose a door. (1, 2 or 3)')
chosenDoor = input()
return chosenDoor
print('You chose door number ' + str(chosenDoor) + '.')
def doors(): # the winning door and the dummy door are randomly selected
winningDoor = random.randint(1,3)
goatDoor = ''
while goatDoor == chosenDoor or goatDoor == winningDoor:
goatDoor = random.randint(1, 3)
def keepOrSwitch():
switchDoor = 1
if switchDoor == chosenDoor or switchDoor == winningDoor:
switchDoor = 2
if switchDoor == chosenDoor or switchDoor == winningDoor:
switchDoor = 3
print('Do you want to')
print('KEEP your choice of door number ' + str(chosenDoor) + '?')
print('...or...')
print('Do you want to')
print('SWITCH and choose door number ' + str(switchDoor) + '?')
print()
choice = ''
while True:
print('Type \'K\' for keep or type \'S\' for switch.')
choice = input()
if choice == 'K' or choice == 'k':
break
if choice == 'S' or choice == 's':
chosenDoor = switchDoor
break
def checkWin():
if chosenDoor == winningDoor:
print('You win!')
if chosenDoor != winningDoor:
print('You lose!')
# the rest of the code is the actual game
playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
chooseDoor()
doors()
keepOrSwitch()
checkWin()
print('Do you want to play again? (yes or no)')
playAgain = input()