我正在尝试学习 Python,但由于全局名称,我在制作一种文本冒险游戏时遇到了一些麻烦,这是我的代码。
def startcave():
print "You awake in an dark cave. You can't remember your past, but you need to find a way out. You feel hungry, although tired."
print "Would you like to delve deeper via a cave passage, or look for a way out in this room?"
answer = raw_input("Do you go -Deeper-, -Search-, or -Rest-?").lower()
if answer == "Deeper" or answer == "deeper":
lowcave1()
elif answer == "Search" or answer == "search":
searchcave()
elif answer == "Rest" or answer == "rest":
caverest()
else:
print "You must either say -Deeper- or -Search-"
startcave()
startcave()
def lowcave1():
print "You go deeper into the cave, walking down a steadily reclining corridor for some time, you have lost track of time."
print "There is a large ledge before you. You can jump down, but there is no way back up."
print "The cave may be dangerous, and you could end up being trapped. Do you turn back?"
answer = raw_input("Do you -Jump- or -Turn back-?").lower()
if answer == "Jump" or answer == "jump":
lowcave2()
elif answer == "Turn back":
caverest2()
lowcave1()
如果我回答“更深”,则会发生此错误。
Traceback (most recent call last):
File "C:\Users\Student\Desktop\Cavestuff.py", line 15, in <module>
startcave()
File "C:\Users\Student\Desktop\Cavestuff.py", line 6, in startcave
lowcave1()
NameError: global name 'lowcave1' is not defined