gpt4 book ai didi

python - 如果答案无效则重新启动 if 循环

转载 作者:行者123 更新时间:2023-12-01 07:34:51 25 4
gpt4 key购买 nike

我有以下代码,如果触发了else,我该如何继续让他们再次输入而不是退出循环?

尝试了一些真正的东西,但无法让它发挥作用。

if awaken in ["a", "stand up"]:
print("Holy...")
elif awaken in ["b", "take a breath"]:
print("Just more text holding")
elif awaken in ["c", "go back to sleep"]:
print("")
else:
print("I don't understand your answer... try again")

最佳答案

将其全部放入 while 循环中。

while True:
awaken = input("Enter command: ").strip() # I presume that you are taking input from user
if awaken in ["a", "stand up"]:
print("Holy...")
break
elif awaken in ["b", "take a breath"]:
print("Just more text holding")
break
elif awaken in ["c", "go back to sleep"]:
print("")
break
else:
print("I don't understand your answer... try again")

输出:

Enter command: x
I don't understand your answer... try again
Enter command: y
I don't understand your answer... try again
Enter command: a
Holy...

不要忘记使用break

我不确定为什么你的最初不起作用,但上面的循环应该可以完成这项工作。

关于python - 如果答案无效则重新启动 if 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57032064/

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