【问题标题】:How can I reloop through an if statement如何通过 if 语句重新循环
【发布时间】:2021-03-28 12:30:08
【问题描述】:

这是我的代码。因此,如果答案不是 1 或 2。脚本应该重新提出问题。但由于某种原因,这不起作用。

question = input("Choose option 1 or 2: ")

if question == '1':
   pass
elif question == '2':
   pass
else:
   while question != '1' or question != '2':
      print("Error. You didn't select a valid option")
      question = input("Choose option 1 or 2: ")

这是怎么回事?如果我第一次得到错误答案,第二次得到正确答案,为什么它不退出 while 循环?

【问题讨论】:

  • 因为如果答案是 1,那么它就不是 2。如果答案是 2,那么它就不是 1。
  • 应该是question != '1' and question != '2'
  • 那行不通。如果我选择 1 作为答案,它会通过,但如果我选择 2,它会再次循环返回

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


【解决方案1】:

您可以将代码简化为:

question = input("Choose option 1 or 2: ")

while question not in ['1', '2']:
    print("Error. You didn't select a valid option")
    question = input("Choose option 1 or 2: ")

如果你使用的是 Python 3.8+,你可以进一步减少它:

while question := input("Choose option 1 or 2: ") not in ['1', '2']:
    print("Error. You didn't select a valid option")

选择'1''2' 后,您可以相应地处理这些案例:

if question == '1' do_stuff() else do_other_stuff()

def do_stuff():
    pass


def do_other_stuff():
    pass

【讨论】:

  • 这也将 '12' 作为答案
  • 啊,真的!忘记了。
【解决方案2】:

只需将 if 语句放在 while 循环中!

question = input("Choose option 1 or 2: ")

while (question != '1' or question != '2'):
    print("Error. You didn't select a valid option")
    question = input("Choose option 1 or 2: ")
    if question == '1' or question == '2':
        break

【讨论】:

    【解决方案3】:

    试试这个:

    while True:
        question = int(input("Choose option 1 or 2: "))
        if question == 1:  #Check if question is equal to 1
            # Do something
            break
        elif question == 2: #Check if question is equal to 2
            #do something
            break
        else:
            print("Number must be 1 or 2.") # " Raise error "
            continue # Continue the loop
    
    

    控制台输出

    >>> Choose option 1 or 2: 3
    >>> Number must be 1 or 2.
    >>> Choose option 1 or 2:
    
    

    【讨论】:

    • 在这种情况下,“继续”是多余的。
    【解决方案4】:

    这里您的代码正在循环,但没有办法回到第一个 if else 语句... 所以在while True: 中使用break 是一种更好的方法:

    while True:
        question = input("Choose option 1 or 2: ")
        if question in ['1','2']:
            break
        print('Error. You didn"t select a valid option')
    print('done!')
    

    控制台:

    Choose option 1 or 2: >? 3
    Error. You didn"t select a valid option
    Choose option 1 or 2: >? 2
    done!
    

    【讨论】:

    • 我认为你应该删除循环内的最后一行。这是第一次的重复。
    • 是的,我是这么认为的。
    • 是的,我一直这样做!
    【解决方案5】:

    你可以这样做:

    while (question!='1' and question!='2'):
        if question == '1':
            print ('You selected 1')
        elif question == '2':
            print('You selected 2')
        else:
            print('Error. You didn"t select a valid option')
            question = input("Choose option 1 or 2: ")````
    

    【讨论】:

    • AND 大部分是 and 以便 python 将其识别为关键字
    猜你喜欢
    • 2013-12-29
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多