【发布时间】:2014-09-23 19:20:29
【问题描述】:
我正在研究“Learn Python the hard way”,并且对 while 循环和布尔运算符有一点理解问题。
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."
只要我输入“嘲讽熊”,脚本就会进入下一个“步骤”。然后我可以输入“开门”,然后继续下一个功能。
但是,在false 出现之前,while 循环是否应该无限运行?在“嘲讽熊”之后发生的事情是bear_moved 被设置为True。那如何才能进行下一步。此外,我不理解and not bear_moved 声明。不应该将bear_moved 设置为假吗?但它已经设置为false。这让我很困惑。
感谢您的任何解释。
【问题讨论】:
-
这是您的代码还是通过书籍/老师/等提供的某些代码?
-
if choice == 'taunt bear' and not bear_moved的意思正是它所说的:“如果选择是taunt bear并且熊没有被移动”。 -
@jonrsharpe:这不是骗局。他没有这个问题。事实上,他有工作代码,但就是不明白。
-
查看代码,
dead函数接受一个参数并调用sys.exit,这将打破循环并结束游戏,考虑到你已经死了,这很有意义;)
标签: python while-loop boolean