我正在制作一个游戏,让你从三个洞穴中选择,每个洞穴里都有一条龙。当我运行这个程序时,它拒绝运行,并说potatocave 是未定义的。我正在使用python 3。我需要知道为什么它不接受定义的potatocave,我做错了什么,以及是否有更简单的方法来做到这一点。
编辑:我再次运行它,它说 selectedCave 是未定义的。回溯错误说:
Traceback (most recent call last):
File "C:\Python33\Projects\Dragon.py", line 32, in <module>
if chosenCave == str(friendlyCave):
NameError: name 'chosenCave' is not defined
import random
import time
time.sleep (3)
def displayIntro():
print('You are in a land full of dragons. In front of you,')
print('you see three caves. In one cave, the dragon is friendly')
print('and will share his treasure with you. Another dragon')
print('is greedy and hungry, and will eat you on sight.')
print('The last dragon is a Potarian and gives free potatoes.')
def chooseCave():
cave = ''
while cave != '1' and cave != '2' and cave != '3':
print('Which cave will you go into? (1, 2 or 3)')
cave = input()
return cave
def checkCave(chosenCave):
print('You approach the cave...')
time.sleep(2)
print('It is dark and spooky...')
time.sleep(2)
print('A large dragon jumps out in front of you! He opens his jaws and...')
print()
time.sleep(2)
friendlyCave = random.randint(1, 3)
potatocave = random.randint(1, 3)
while potatocave == friendlyCave:
potatocave = random.randint(1, 3)
if chosenCave == str(friendlyCave):
print('Gives you his treasure!')
elif chosenCave == str(potatocave):
print ('Millions of potatoes rain from the sky.')
else:
print('Gobbles you down in one bite!')
playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
displayIntro()
caveNumber = chooseCave()
checkCave(caveNumber)
print('Do you want to play again? (yes or no)')
playAgain = input()
PS这不是最终版本,我只是用一个土豆洞作为占位符来弄清楚三个洞穴的概念,而不是我原来的两个。