python - 初学者 Python : How to give different responses to ELSE in a while-loop

标签 python python-2.7

因此,我正在学习艰难地学习 Python,我正在学习第 36 课,我根据他在第 35 课中所做的游戏制作了我自己的 BBS 文本风格的游戏。

http://learnpythonthehardway.org/book/ex36.html

http://learnpythonthehardway.org/book/ex35.html

所以,我做得不错,但我注意到,当我模仿他永恒的 while 循环时,玩家在任何情况下都不能离开房间,除非是非常特殊的情况,循环总是对否则……除非他们说的是正确的话,否则他们将永远陷入困境。

这是我的代码:

def sleeping_giant():
    print "There is a single door, with a giant sleeping in front of it."
    print "How can you get the giant to move?"
    print "Should you wake him?"
    giant_moved = False

    while True:
        choice = raw_input("> ")

        if choice == "Yes":
            dead("The giant rips you into four pieces and uses your quartered body as a      stack of pillows.")
        elif choice == "No" and not giant_moved: 
            print "The giant rolls in his sleep, clearing an easy path to the door."
            giant_moved = True 
        elif "Open" or "Go" in choice and giant_moved:
            treasure_room()
        else:
            print "I have no idea what the fuck you are trying to say to me.  English.     Do you speak it?!"

如果该格式翻译不好,我们深表歉意。

无论如何,只要用户键入不满足 if 或 elif 的内容,他们就会收到相同的 else 响应。

我该如何改变它?比如,让它更具动态性,这样如果他们继续搞砸响应,其他响应就会改变?

我不知道如何让代码说(用非文字术语,我的意思是,我无法让逻辑说),'如果使用了 else,响应应该是新的 else,一旦使用了它,它应该是另一个 else 响应。

如果这没有意义,请告诉我。

最佳答案

这是 Greg 使用计数器的增量版本的答案,因此您可以获得可预测的响应顺序:

global responses = [ "Nice try. Try again.",
        "Sorry, what was that?",
        "I don't know what that means."]
def sleeping_giant():
   counter = 0
   print "There is a single door, with a giant sleeping in front of it."
   print "How can you get the giant to move?"
   print "Should you wake him?"
   giant_moved = False

   while True:
       choice = raw_input("> ")

       if choice == "Yes":
           dead("The giant rips you into four pieces and uses your quartered body as a      stack of pillows.")
       elif choice == "No" and not giant_moved: 
           print "The giant rolls in his sleep, clearing an easy path to the door."
           giant_moved = True 
       elif ("Open" in choice or "Go" in choice) and giant_moved:
           treasure_room()
       else:
           print responses[counter]
           if counter < 2:
              counter += 1

关于python - 初学者 Python : How to give different responses to ELSE in a while-loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25193968/

相关文章:

Python 命令行 args 格式问题

python - 正则表达式提取多行散列注释

python - distil 网络机器人检测的工作

python - 检测 python 中的空函数定义

python : Extend a copy of a list - Bug?

Python简单的网络服务器

python - 如何检测参数网格中允许哪些值?

python - 我如何解码这个表示为 unicode 的字符串?

python-2.7 - Pandas:根据百分位条件过滤数据框

python - Tcl 错误 : bad geometry specifier