用户按下所有答案后,如何在python中结束循环?

时间:2018-11-12 17:01:47

标签: python loops

# 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似乎无效。我希望循环在用户输入所有三个选项后结束;年龄,笑话和事实。

2 个答案:

答案 0 :(得分:5)

您可以将用户查询的状态记录在一个集中:

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")

答案 1 :(得分:1)

您发布的代码没有做任何事情来跟踪已选择的项目。您的循环条件不会检查何时应退出。通常,您希望您的SELECT U.*, J.id_Module, M.name AS name_Module, J.id_Rule, R.name AS name_Rule FROM dbo.[Users] U (nolock) JOIN dbo.j_Users_Modules_Rule J (nolock) ON U.id = J.id_User JOIN dbo.Modules M (nolock) ON M.id = J.id_Module JOIN dbo.Modules_Access_Rules R (nolock) ON R.id = J.id_Rule 条件控制循环退出;此应用程序不保证使用根深蒂固的while

我建议您设置三个标志(布尔变量),每个选项一个。看到每个项目时,设置适当的标志。这是循环头和一个子句的附加示例:

break

你能从那里拿走吗?