0

我正在尝试学习 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
4

3 回答 3

2

使用 global 关键字表示您的意思是使用全局变量。

foo = 123

def bar():
    global foo
    print(foo)
于 2013-09-17T15:35:23.490 回答
1

您需要在调用它们之前定义所有函数

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()

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()

startcave()

lowcave1()
于 2013-09-17T15:42:57.810 回答
0

好的,你应该使用global.

a = 1
print '1: ', a # Prints 1: 1

def f1():
  a = 2
  print '2: ', a

f1() # Prints 2: 2

print '3: ', a # Prints 3: 1

def f2():
  global a
  a = 3
  print '4: ', a

f2() # Prints 4: 3

print '5: ', a # Prints 5: 3
于 2013-09-17T15:37:06.053 回答