2013-05-21 62 views
1

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

任何幫助將不勝感激。

+1

究竟不起作用? – 2013-05-21 10:20:01

+0

你好Lutz。在我編輯的節目中,如果我不止一次嘲笑這隻熊,我所得到的只是「熊正在生氣,不要再嘲笑他。」字符串,一遍又一遍。 P.S .:我編輯了這個問題,包括這個。 – user2331291

+1

'not bear_moved'不會修改任何內容 - 這是一個表達式,如果bear_moved不計算爲True,則返回True,否則返回False。 'bear_moved = not bear_moved'會產生你想象中的效果,但這是一項任務,所以它不可能成爲'if'條件的一部分。 – kampu

回答

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 ==」嘲諷熊「和bear_moved:」在「elif next ==」嘲笑熊「和bear_moved_again」之前進行了測試。如果你輸入「嘲諷熊」很多次,「ELIF未來==‘嘲諷熊的一個’,而不是bear_moved」和「ELIF未來==‘嘲諷熊’和bear_moved」將總是是真實的。測試,「elif next ==」嘲諷熊「和bear_moved_again」,將永遠不會被採取。

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 

不知道你的第二個問題是說什麼,但在這一行沒有變量賦值。這只是檢查一個斷言是否是這樣,而不是改變任何東西。

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  

它可能不太可讀雖然。

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." 
+0

謝謝gnibbler,雖然不完全解釋'爲什麼'它不起作用,但您提供了一個優雅的(在我的非常謙虛的意見)替代代碼。謝謝! – user2331291