Python(学习python艰难的练习方式35)

时间:2013-05-21 10:17:45

标签: python python-2.7

我目前正在研究Zed Shaw的艰难学习Python。

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设置为True?

非常感谢任何帮助。

4 个答案:

答案 0 :(得分: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“和bear_moved:”的句子在“elif next ==”taunt bear“and bear_moved_again”之前进行测试。如果您多次输入“嘲讽熊”,其中一个“elif next ==”嘲讽熊“而不是bear_moved”和“elif next ==”taunt bear“和bear_moved”将始终成立。测试,“elif next ==”taunt bear“和bear_moved_again”将永远不会被拍摄。

答案 1 :(得分:2)

问题是,当您嘲笑熊两次时,bear_moved仍为true,因此每次程序处理条件时都会触发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

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

答案 2 :(得分: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    

虽然可能不太可读。

答案 3 :(得分: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."