Python - 局部变量错误

时间:2015-02-03 12:50:29

标签: python function if-statement

当我在Pwershell中运行此代码时:

def treasure_room():            
    print "There is a pot of gold as high as you are tall, surrounded by     pillars."
    print "There is a sack by your feet. What do you do?"

    sack = raw_input(prompt)
    if sack == "pick up the sack" or sack == "pick it up" or sack == "pick up":
        print "You pick up the sack."   
        nowsack()               
    else:
        print "How will you carry the gold?!"
        treasure_room()


    def nowsack():
        print "The pot is in front of you and you have the sack. Now what?"

        gopot = raw_input(prompt)
        if gopot == "walk to the pot" or gopot == "walk to it":
            print "Now you are next to the pot. The pillars surround it and you..."
        else: 
            "You continue to stand where you are, with the gold in front of you..."
             nowsack()


treasure_room()

我在提示中运行它,并在第二个打印功能问我“你做什么?”后输入“捡起来”。

然后我在Powershell中收到以下错误:

Traceback (most recent call last):
    File "ex36game.py", line 58, in <module>
        treasure_room()
File "ex36game.py", line 38, in treasure_room
    nowsack()
        UnboundLocalError: local variable 'nowsack' referenced before assignment

任何人都知道为什么?我想也许我应该把定义nowsack()函数放在第一个打印注释“有一罐金币......”上面并用8个空格缩进该函数,但我尝试了这个并且不断出现错误(不同类型。 )

1 个答案:

答案 0 :(得分:2)

你需要 unindent nowsack()函数定义;将整个区块向左移动4个空格:

def treasure_room():            
    print "There is a pot of gold as high as you are tall, surrounded by     pillars."
    print "There is a sack by your feet. What do you do?"

    sack = raw_input(prompt)
    if sack == "pick up the sack" or sack == "pick it up" or sack == "pick up":
        print "You pick up the sack."   
        nowsack()               
    else:
        print "How will you carry the gold?!"
        treasure_room()

def nowsack():
    print "The pot is in front of you and you have the sack. Now what?"

    gopot = raw_input(prompt)
    if gopot == "walk to the pot" or gopot == "walk to it":
        print "Now you are next to the pot. The pillars surround it and you..."
    else: 
        "You continue to stand where you are, with the gold in front of you..."
         nowsack()

正如您现在缩进它一样,它是treasure_room函数的一部分,因此在之后定义的本地在函数中使用它。通过取消缩进它将成为一个全局,并在导入模块文件时创建该函数;它会在treasure_room()运行时可用。

替代方法是将nowsack()函数定义移动到treasure_room()函数的顶部,以确保在使用它之前将其定义为嵌套函数:

def treasure_room(): 
    def nowsack():
        print "The pot is in front of you and you have the sack. Now what?"

        gopot = raw_input(prompt)
        if gopot == "walk to the pot" or gopot == "walk to it":
            print "Now you are next to the pot. The pillars surround it and you..."
        else: 
            "You continue to stand where you are, with the gold in front of you..."
             nowsack()

    print "There is a pot of gold as high as you are tall, surrounded by     pillars."
    print "There is a sack by your feet. What do you do?"

    sack = raw_input(prompt)
    if sack == "pick up the sack" or sack == "pick it up" or sack == "pick up":
        print "You pick up the sack."   
        nowsack()               
    else:
        print "How will you carry the gold?!"
        treasure_room()