我目前正在研究Zed Shaw的Python Python学习方法。Python(学python硬方法练习35)
在exercise 35,人们发现方案,其中包括这些行:
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 the 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."
所有好。但是我想让球员有一个额外的机会在比赛失败之前生存下来并发出警告。我想出了这个:
def bear_room():
print "There is a bear here."
print "There 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
bear_moved_again = 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 as moved from the door. You can go through it now."
bear_moved = True
elif next == "taunt bear" and bear_moved:
print "The bear is getting angry. Don't taunt him again."
bear_moved_again = True
elif next == "taunt bear" and bear_moved_again:
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(6号线),和(13号线)说:
elif next == "taunt bear" and not bear_moved:
会不会 “而不是” 设置bear_moved为真?
任何帮助将不胜感激。
究竟不起作用? – 2013-05-21 10:20:01
你好Lutz。在我编辑的节目中,如果我不止一次嘲笑这只熊,我所得到的只是“熊正在生气,不要再嘲笑他。”字符串,一遍又一遍。 P.S .:我编辑了这个问题,包括这个。 – user2331291
'not bear_moved'不会修改任何内容 - 这是一个表达式,如果bear_moved不计算为True,则返回True,否则返回False。 'bear_moved = not bear_moved'会产生你想象中的效果,但这是一项任务,所以它不可能成为'if'条件的一部分。 – kampu