如何定义全局名称?

时间:2013-09-17 15:32:05

标签: python

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

3 个答案:

答案 0 :(得分:2)

使用global关键字表示您使用全局变量。

foo = 123

def bar():
    global foo
    print(foo)

答案 1 :(得分: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()

答案 2 :(得分: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