我目前正在研究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