python - 当我运行解码代码时,循环继续上一个问题
问题描述
我正在尝试使用while循环在python中进行文本游戏挑战,但是当我完成游戏时,它仍然会继续上一个问题,我希望它在用户回答最后一个问题时停止。
question = ""
while question.lower() != "yes" and question.lower() != "no":
question = input("Do you want to play?\n")
print()
if question.lower() == "yes":
while question.lower() != "bottle" and question.lower() != "castle":
question = input(f"You are alone in a island and you saw a BOTTLE and a CASTLE\n Which one do you choose?\n ")
print()
if question.lower() == "bottle":
while question.lower() != "sea" and question.lower() != "trees":
question = input("The bottle has a message: \"No one leaves this island without proving their worth to themselves\", you can try the SEA or the TREES\n Which one do you choose\n")
print()
if question.lower() == "sea":
print("Is already night, and you were ate by a whale \U0001F40B in the blink of an eye")
print()
break
else:
……
elif question.lower() == "no":
print("That´s too bad!")
print()
else:
print("Invalid answer, please try again")
print()
解决方案
导入 sys 并使用 sys.exit() 代替 break 语句。代码不起作用,因为这些 break 语句只中断内部 while 循环而不是外部循环。
推荐阅读
- firebase - 如何使用复选框单击更改 Firebase 中子项的值
- javascript - 如何根据本地存储中的值显示/隐藏 div?
- php - 如何在 laravel 中访问数组值
- c# - 测试异步方法的超时异常
- api - 如何使用 SSH 隧道以编程方式与 Restful API 通信
- xml - 按属性对 XML 进行排序
- reactjs - 重定向在 React 中无法正常工作
- python - 错误:grpc._plugin_wrapping:AuthMetadataPluginCallback 引发异常!(gunicorn) 使用 eventlet (async) 工作者时
- sumo - SUMO 的多模式活动生成器 (SAGA) - 运行时出现 KeyError
- java - 在 Talend 中将两个条目行(开始时间和结束时间)连接为单行