python - 用户按下所有答案后如何在python中结束循环?
问题描述
# loop
while True:
inp = raw_input()
if inp == "":pres= input("What would you like to know about me? AGE, JOKE, FACT")
if pres in {'AGE', 'Age','age'}:
print("I was birthed from my mother 87 years ago. Press enter to continue")
if pres in {'JOKE','Joke','joke'}:
print("Where do polar bears keep their money?")
import time
time.sleep(2)
print("In a snow bank! Press enter to continue")
if pres in {'FACT','Fact','fact'}:
print("Hippopotamus's have pink spit! Press enter to continue")
# end of loop
我试图结束这个循环,但最后使用 break 似乎不起作用。我希望在用户输入所有三个选项后结束循环;年龄、笑话和事实。
解决方案
您可以在一组中记录用户查询的状态:
chosen = set()
while len(chosen) < 3:
inp = raw_input()
if inp == "":
pres = input("What would you like to know about me? AGE, JOKE, FACT")
if pres in {'AGE', 'Age','age'}:
print("I was birthed from my mother 87 years ago. Press enter to continue")
chosen.add("age")
if pres in {'JOKE','Joke','joke'}:
print("Where do polar bears keep their money?")
import time
time.sleep(2)
print("In a snow bank! Press enter to continue")
chosen.add("joke")
if pres in {'FACT','Fact','fact'}:
print("Hippopotamus's have pink spit! Press enter to continue")
chosen.add("fact")
推荐阅读
- c++ - 如何修复这个未声明的标识符?(C++)
- javascript - ReactTable 上页面大小的自定义下拉菜单
- django - 表单集未在 Django 3 中显示
- node.js - 如何使用 GSuite 帐户通过 node.js 发送邮件?
- java - 在另一个 Java 程序中执行程序
- jquery - jQuery slideToggle 不在实时站点上显示动画,而是在 jsfiddle 上
- matplotlib - 在 matplotlib 中创建条形图
- javascript - 一个人选中 3 个复选框后,如何使按钮解锁?
- javascript - Javascript 正则表达式:无法删除多行字符串中的前瞻组中的前导空格
- c# - 使用 c# 是否可以检测任务栏何时更改状态?