我目前正在研究 Zed Shaw 的 Learn Python the Hard Way。
在练习 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 设置为 True 吗?
任何帮助将不胜感激。