2013-05-21 149 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