1

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

任何帮助将不胜感激。

4

4 回答 4

2

换行

elif next == "taunt bear" and bear_moved:

elif next == "taunt bear" and bear_moved and not bear_moved_again:

和线

elif next == "taunt bear" and bear_moved_again:

elif next == "taunt bear" and bear_moved and bear_moved_again:

在您的原始版本中,句子“elif next == "taunt bear" and bear_moved:" 在 "elif next == "taunt bear" and bear_moved_again" 之前进行了测试。如果您多次输入“taunt bear”,“elif next == "taunt bear" and not bear_moved" 和 "elif next == "taunt bear" and bear_moved" 之一将始终为真。永远不会进行“elif next ==”taunt bear”和bear_moved_again”的测试。

于 2013-05-21T10:29:05.200 回答
2

问题是bear_movedtrue你两次嘲弄熊时,这行elif next == "taunt bear" and bear_moved:仍然是每次程序处理条件时都会触发的。永远不会到达与代码相关的行,bear_moved_again因为它在代码中位于它之后。

如果将以前的分支更改为以下内容,则代码应该可以工作:

elif next == "taunt bear" and bear_moved:
    print "The bear is getting angry. Don't taunt him again."
    bear_moved_again = True
    bear_moved = False

不确定您在第二个问题中的确切含义,但是这一行中没有变量赋值。它只是检查一个断言是否是这种情况,而不是改变任何东西。

于 2013-05-21T10:31:11.867 回答
1

一个不费吹灰之力的方法是交换这两个elif子句,即

改变

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 == "taunt bear" and bear_moved_again:
   dead("The bear gets pissed off and chews your leg off.")
elif next == "taunt bear" and bear_moved:
   print "The bear is getting angry. Don't taunt him again."
   bear_moved_again = True    

虽然它可能不太可读。

于 2013-05-21T10:37:45.467 回答
1

您可以将 bear_moved 设为一个int并计算熊移动的次数

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 = 0

    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 += 1
        elif next == "taunt bear" and bear_moved == 1:
            print "The bear is getting angry. Don't taunt him again."
            bear_moved += 1   
        elif next == "taunt bear" and bear_moved == 2:
            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."
于 2013-05-21T10:42:37.030 回答