gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:19:07 25 4
gpt4 key购买 nike

因此,我正在学习艰难地学习 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com