[英]True & False Boolean in Python
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 the door. You can go through."
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.
This is to show my understanding of Boolean. 这是为了显示我对布尔的理解。 In the line testing next == "taunt bear" and not bear_moved
, if my input is taunt bear
, the result is True and True
, which will continue the loop. next == "taunt bear" and not bear_moved
的行测试中,如果我的输入是taunt bear
,结果为True and True
,它将继续循环。
So what confused me is line testing next == "taunt bear" and bear_moved
. 所以让我感到困惑的是, next == "taunt bear" and bear_moved
线测试next == "taunt bear" and bear_moved
。 If my input is taunt bear
, is it suppose to be "taunt bear" == "taunt bear"
and bear_moved
which is True and True
? 如果我的输入是taunt bear
,是否应该是"taunt bear" == "taunt bear"
和bear_moved
,分别为True and True
? which means the loop will continue instead of cancelling it. 这意味着循环将继续而不是取消循环。
while True:
will continue indefinitely (unless you put in a break
- usually break
is bad design in languages like Python)). while True:
会无限期继续(除非您稍作break
-通常, break
是Python等语言中的不良设计)。
You probably want to do: 您可能想做:
while something == True: #Or - as has been pointed out, `while something` - which is equivalent and more Pythonic
...
some code which eventually sets something to False
...
elif
in python is equivilent to else if
. python中的elif
等价于else if
。 If one of the elif
blocks execute, none of the others can, and will jump to the bottom of the if
. 如果执行其中一个elif
块,则其他任何一个都不能执行,并将跳至if
的底部。
After next == "taunt bear" and not bear_moved
is executed and evaluates to true, and the block executes, control of the program continues back at the front of the loop and will ask for input again. 在执行next == "taunt bear" and not bear_moved
并评估为true,然后执行该块后,对程序的控制在循环的最前面继续,并再次请求输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.