【问题标题】:Why is my while not loop with two conditions only using the first condition and ignoring the second condition?为什么我的 while 不使用两个条件循环仅使用第一个条件而忽略第二个条件?
【发布时间】:2020-12-14 17:26:43
【问题描述】:

我正在尝试设置一个使用“或”两个条件的 while not 循环。本质上,我要求用户输入贷款类型,如果没有输入两个贷款之一,它会打印出它不是有效的贷款类型并再次要求输入。但是,我的代码仅适用于第一个条件,而第二个条件被忽略。

下面是我的代码:

loan = input('Enter the type of mortgage loan for the purchase of this property: \n').lower() #Asks for the type of mortgage loan
while not loan == 'fha' or loan == 'conventional': #Checks if input is fha or conventional, if not asks for input again
    print("Sorry, that is not a valid loan type.")
    loan = input('Enter the type of mortgage loan for the purchase of this property: \n').lower()

感谢您的帮助!

【问题讨论】:

    标签: python while-loop conditional-statements


    【解决方案1】:

    一个不那么令人困惑的选择是:

    loan = input('Enter the type of mortgage loan for the purchase of this property: \n').lower() #Asks for the type of mortgage loan
    while loan not in ("fha", "conventional"):
        print("Sorry, that is not a valid loan type.")
        loan = input('Enter the type of mortgage loan for the purchase of this property: \n').lower()
    

    【讨论】:

    • 这正是我想要的效果!非常感谢我想知道如何设置它来检查输入是否在列表/元组中
    • 您也可以将其放在变量中,例如loan_types = ("fha","conventional")。另一种选择是使用 list loan_types=["fha","conventional"] 来实现可变性。
    猜你喜欢
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2018-12-22
    • 2018-08-13
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    相关资源
    最近更新 更多