学习Python艰难的方法练习35布尔表达式

时间:2014-09-26 23:16:08

标签: python

我想知道如何划线

elif choice == "taunt bear" and not bear_moved:
    print "The bear has moved from the door. You can go through it now."
    bear_moved = True

......和......

elif choice == "taunt bear" and bear_moved:
    dead("The bear gets pissed off and chews your leg off.")

...实现变量bear_moved?它已在False循环之前定义为while,我想知道为什么将它与"选择"中输入的内容进行比较。以及如何在您输入答案时检查它。此外,如果它说" not" bear_moved,不能使变量成立吗?那为什么它被定义为真呢?

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:
        choice = raw_input("> ")

        if choice == "take honey":
            dead("The bear looks at you then slaps your face off.")
        elif choice == "taunt bear" and not bear_moved:
            print "The bear has moved from the door. You can go through it now."
            bear_moved = True
        elif choice == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        elif choice == "open door" and bear_moved:
            gold_room()
        else:
            print "I got no idea what that means."

3 个答案:

答案 0 :(得分:2)

bear_moved未与choice进行比较。仅当choice == "taunt bear"为真时才会测试布尔值

自己上的测试if bear_moved: 完全基于bear_moved的值为真或假。因此,如果bear_moved = False,则测试if bear_moved:将失败。如果bear_moved = True,那么if bear_moved:将会成功:

>>> bear_moved = False
>>> if bear_moved:
...     print('The bear moved')
... 
>>> bear_moved = True
>>> if bear_moved:
...     print('The bear moved')
... 
The bear moved

not颠倒了测试;现在,当您将其设置为bear_moved = False时,if not bear_moved:将会成功,反之亦然:

>>> bear_moved = False
>>> if not bear_moved:
...     print('The bear has not moved')
... 
The bear has not moved
>>> bear_moved = True
>>> if not bear_moved:
...     print('The bear has not moved')
... 
>>> 

回到choice,测试首先查看choice == "taunt bear"。如果确实如此,那么然后 and也会测试bear_moved。因此,要使if choice == "taunt bear" and bear_moved:成功,必须确保两件事:choice必须设置为"taunt bear" 并且 bear_moved的值必须为true。

while循环中,第一次bear_moved设置为False。如果您选择taunt bear,则测试elif choice == "taunt bear" and not bear_moved:将成立:choice == "taunt bear"为真, not bear_moved为真,因为{{1}是bear_moved。系统会打印一条消息,False现已更改为bear_moved

True循环继续,您可以再次选择while,但这次"taunt bear"设置为bear_moved。测试True不会成功,因为即使elif choice == "taunt bear" and not bear_moved:为真,choice == "taunt bear"部分也不是真的!但是,另一个测试not bear_moved现在是真的,因此你就死了。

顺便说一下,elif choice == "taunt bear" and bear_moved:时刻已经执行,bear_moved = True测试可用,只要您使用elif choice = "open door" and bear_moved:作为选择。

答案 1 :(得分:1)

我认为你在这里误解了评估或比较运算符。

elif choice == "taunt bear" and not bear_moved:

这句话的内容如下:

if choice is the text "taunt bear"
and
the boolean variable bear_moved is False

它也没有改变bear_moved;它只是检查它是否为假。

答案 2 :(得分:1)

让我们一步一步。

当您开始循环时,由于bear_moved行,Falsebear_moved = False

所以,它要求你做出选择,你输入taunt bear,它就到了那一行:

    elif choice == "taunt bear" and not bear_moved:

显然是choice == "taunt bear",因为那是你输入的内容。

至于not bear_moved,它不会修改bear_moved,它只是意味着"使用与bear_moved"中相反的内容。因此,由于bear_movedFalse,这意味着not bear_movedTrue

由于and的两边都是True,所以整件事都是True。因此,它打印出消息,并设置bear_moved = True

现在,下次进行循环时,再次输入taunt bear,它会到达同一行。

这一次,因为您设置了bear_moved = Truebear_moved现在是True,所以not bear_moved现在是False。由于and的一边是False,所以整个事情都是False,所以它会跳过这一行并转到下一行。

在下一个测试中,choice == "taunt bear"仍然是True,而bear_moved也是True,所以现在你死了。