【问题标题】:Returning to a different line than the starting point within a while True-loop在一段时间内返回与起点不同的行
【发布时间】:2021-07-06 16:25:21
【问题描述】:

编辑:问题已在 cmets 中解决。没有试过勾选的答案。

当给出无效答案时,我使用了一个 while True-loop 来返回起点。这对我的第一个 if-elif-else-statement 非常有用。但是,如果我想要第二个 if-elif-else-statement,在前面的条件之一中,我无法弄清楚如何将无效答案发送回第二个 if-elif-else-statement 而不是第一个的开头。这是我的代码(问题以粗体显示):

while True:
    a = input("Do you want Stackoverflow to help me with this problem? Yes/No: ")
    if a == "Yes":
        b = input("I need a second if-statement to make my point. Is that ok? Yes/No: ")
        if b == "Yes":
            print("How very nice of you. My problem is with the else condition")
            break
        elif b == "No":
            print("why not? I'm not asking for much")
            break
        else:
            print("Invalid answer. Write Yes/No.") #**Here is my problem. I want an invalid answer to return to line 4, not line 2.**
    elif a == "No":
        print("Ok. Thats too bad for me")
        break
    else:
        print("Invalid answer. Write Yes/No.")

【问题讨论】:

  • 在第 4 行添加另一个 while True 然后修复其余代码的缩进
  • @mhhabib 这修复了它。我以为我已经尝试过这个并得到了错误,但我想不是。可能忘了修复缩进。谢谢!
  • 查看代码,将其分解为几个函数可能是值得的。一种选择是将第二个循环放在一个单独的函数中,特别是如果你能给它一个好名字的话。另一个好方法是创建一个单独的函数,通过循环询问是/否问题,这样主代码就可以处理是/否,而无需担心循环。

标签: python python-3.x while-loop


【解决方案1】:

这可能不是有效的解决方案,但很容易理解和实施。

exit_first_loop = False
exit_second_loop = False

while not exit_first_loop:
    a = input("Do you want Stackoverflow to help me with this problem? Yes/No: ")
    if a == "Yes":
    
        while not exit_second_loop:
            b = input("I need a second if-statement to make my point. Is that ok? Yes/No: ")
            if b == "Yes":
                print("How very nice of you. My problem is with the else condition")
                exit_first_loop = True
                exit_second_loop = True
            elif b == "No":
                print("why not? I'm not asking for much")
                exit_first_loop = True
                exit_second_loop = True
            else:
                print("Invalid answer. Write Yes/No.") #**Now you will be sent to 4.**
    elif a == "No":
        print("Ok. Thats too bad for me")
        exit_first_loop = True
    else:
        print("Invalid answer. Write Yes/No.")


【讨论】:

    【解决方案2】:

    来自@mhhabib 评论: 在第 4 行添加另一个 while True 然后修复其余代码的缩进

    我还必须在第 14 行添加一个新的 break-statement 以获得所需的结果,在得到后停止循环。

    固定代码,在#中进行了编辑。

    while True:
        a = input("Do you want Stackoverflow to help me with this problem? Yes/No: ")
        if a == "Yes":
            while True: # added extra while True, and fixed indent.
                b = input("I need a second if-statement to make my point. Is that ok? Yes/No: ")
                if b == "Yes":
                    print("How very nice of you. My problem is with the else condition")
                    break
                elif b == "No":
                    print("why not? I'm not asking for much")
                    break
                else:
                    print("Invalid answer. Write Yes/No.")
            break # added break (one indent back) so first loop doesnt start after second is broken.
        elif a == "No":
            print("Ok. Thats too bad for me")
            break
        else:
            print("Invalid answer. Write Yes/No."```
    

    【讨论】:

      【解决方案3】:

      在代码组织方面,将代码重新组织成单独的函数可能是值得的;一种可能的分离功能是“循环询问,直到给出有效答案”;那么主代码根本不需要担心重试:

      def ask(question, options):
          options_text = "/".join(options)
          while True:
              ans = input("%s %s:" % (question, options_text)
              if ans in options:
                  return ans
              print("Invalid answer. Write %s" % options_text)
      
      a = ask("Do you want Stackoverflow to help me with this problem?", ("Yes", "No"))
      if a == "Yes":
          b = ask("I need a second if-statement to make my point. Is that ok?", ("Yes", "No"))
          if b == "Yes":
              print("How very nice of you. My problem is with the else condition")
          else:  # b == "No"
              print("why not? I'm not asking for much")
      else:  # a == "No"
          print("Ok. Thats too bad for me")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多