Python 为什么需要这一行代码

Python 为什么需要这一行代码,python,python-2.7,Python,Python 2.7,我从Zed Shaw的《学习python的艰难之路》一书中学习python,我编写了以下代码: def bear_room(): print "There is a bear here." print "The bear has a bunch of honey." print "The fat bear is in front of another door." print "How are you going to move the bear?" be

我从Zed Shaw的《学习python的艰难之路》一书中学习python,我编写了以下代码:

def bear_room():
    print "There is a bear here."
    print "The bear has a bunch of honey."
    print "The fat bear is in front of another door."
    print "How are you going to move the bear?"
    bear_moved = False

    while True:
        next = raw_input("> ")

        if next == "take honey":
            dead("The bear looks at you then slaps your face off.")
        elif next == "taunt bear" and not bear_moved:
            print "The bear has moved from door. You can go through it now."
            bear_moved = True
        elif next == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        elif next == "open door" and bear_moved:
            gold_room()
        else:
            print "I got no idea what that means."
然而,我想知道他为什么使用“bear_moved=False”,因为我运行了几次程序,并且使用了嘲弄bear,我总是得到“熊已经离开了门,你现在可以通过了。”那么代码的第二个“嘲弄熊”部分有什么意义呢


任何帮助都将不胜感激。

我从未读过这本书,但我认为这就是正在发生的事情

"The fat bear is in front of another door."
因此,熊处于其原始位置(在门前),并且尚未移动,从
bear\u moved=False

当你嘲弄熊时,熊就会离开门,如图所示:

 elif next == "taunt bear" and not bear_moved:
        print "The bear has moved from door. You can go through it now."
        bear_moved = True
由于它不再处于原始位置,熊已经移动了,因此我们设置
bear\u moved=true

现在,如果用户在
bear\u moved=true
时再次嘲笑熊,熊会做其他事情:

elif next == "taunt bear" and bear_moved:
        dead("The bear gets pissed off and chews your leg off.")
请注意,在第一个elif中,它表示“嘲弄熊”和“不移动熊”,而在下一个elif中,它表示“嘲弄熊”和“移动熊”。第一个是检查熊是否移动,另一个是检查熊是否移动


希望这能有所帮助:)

我从未读过这本书,但我认为这就是正在发生的事情

"The fat bear is in front of another door."
因此,熊处于其原始位置(在门前),并且尚未移动,从
bear\u moved=False

当你嘲弄熊时,熊就会离开门,如图所示:

 elif next == "taunt bear" and not bear_moved:
        print "The bear has moved from door. You can go through it now."
        bear_moved = True
由于它不再处于原始位置,熊已经移动了,因此我们设置
bear\u moved=true

现在,如果用户在
bear\u moved=true
时再次嘲笑熊,熊会做其他事情:

elif next == "taunt bear" and bear_moved:
        dead("The bear gets pissed off and chews your leg off.")
请注意,在第一个elif中,它表示“嘲弄熊”和“不移动熊”,而在下一个elif中,它表示“嘲弄熊”和“移动熊”。第一个是检查熊是否移动,另一个是检查熊是否移动


希望这有帮助:)

它们是两个不同的状态,有两个不同的参数。其中一个说如果泰格熊和熊动了,那就做点什么,只有熊动了,你动了他,这才会做点什么,而另一个说只有熊不动,你动了他,另一个才会做点什么。明白了吧,它们是两个不同的东西,因为一个不动就会消失,另一个动了就会消失。

它们是两个不同的状态,有两个不同的参数。其中一个说如果泰格熊和熊动了,那就做点什么,只有熊动了,你动了他,这才会做点什么,而另一个说只有熊不动,你动了他,另一个才会做点什么。明白了吗?它们是两个不同的东西,因为如果他不移动,一个会消失,如果他移动,另一个会消失。

这是因为这行中的变量
bear_moved
正在读取(检查是否为false):

elif next == "taunt bear" and not bear_moved:
这意味着它正在被引用。在分配变量之前,不能引用它。因此,输入两次
>taunt bear
,您的程序将转到
dead
,程序将带着给定的消息退出

尝试删除该行,将出现以下错误:

Traceback (most recent call last):
  File "c:\Users\user\Documents\test.py", line 28, in <module>
    bear_room()
  File "c:\Users\user\Documents\test.py", line 18, in bear_room
    elif next == "taunt bear" and not bear_moved:
UnboundLocalError: local variable 'bear_moved' referenced before assignment
回溯(最近一次呼叫最后一次):
文件“c:\Users\user\Documents\test.py”,第28行,在
熊屋
文件“c:\Users\user\Documents\test.py”,第18行,在bear\u room中
elif next==“嘲弄熊”且未移动熊:
UnboundLocalError:赋值前引用了局部变量“bear\u moved”

这说明了我们上面讨论的内容。学习编程时,实验是最好的方法。

这是因为正在读取此行中的变量
bear\u moved

elif next == "taunt bear" and not bear_moved:
这意味着它正在被引用。在分配变量之前,不能引用它。因此,输入两次
>taunt bear
,您的程序将转到
dead
,程序将带着给定的消息退出

尝试删除该行,将出现以下错误:

Traceback (most recent call last):
  File "c:\Users\user\Documents\test.py", line 28, in <module>
    bear_room()
  File "c:\Users\user\Documents\test.py", line 18, in bear_room
    elif next == "taunt bear" and not bear_moved:
UnboundLocalError: local variable 'bear_moved' referenced before assignment
回溯(最近一次呼叫最后一次):
文件“c:\Users\user\Documents\test.py”,第28行,在
熊屋
文件“c:\Users\user\Documents\test.py”,第18行,在bear\u room中
elif next==“嘲弄熊”且未移动熊:
UnboundLocalError:赋值前引用了局部变量“bear\u moved”

这说明了我们上面讨论的内容。在学习编程时,实验是最好的方法。

您是否尝试连续输入两次“嘲讽熊”了?因为您不能在赋值前引用变量
not bear\u moved
总是计算为该变量的相反值。所以最初它是
False
,而
notfalse
意味着它是
True
。请注意,这不会更改该变量的值,因为没有赋值。稍后在同一个条件块中,您可以看到它正在将
True
赋值给
bear\u moved
变量。其他人已经提到了为什么在使用变量之前必须声明变量。您是否尝试连续输入两次“嘲讽熊”了?因为在赋值
未移动
之前,您不能引用变量,因此总是计算结果与该变量相反。所以最初它是
False
,而
notfalse
意味着它是
True
。请注意,这不会更改该变量的值,因为没有赋值。稍后在同一个条件块中,您可以看到它正在将
True
赋值给
bear\u moved
变量。其他人已经提到了为什么在使用变量之前必须声明变量。我没有意识到它会在赋值之前引用变量。这是迄今为止最正确/准确的答案。啊,好吧,这更有意义。谢谢大家!@博布伦