我的第一个if语句总是正确的.没有引发错误或异常



看起来我的第一个if语句不起作用,总是为yes。我想在我的计算程序中提供选择,无论用户是否使用公制。

在那之后一切都会好起来的。感谢您的支持。

def bmi_calc():
    question = input('Do you use metric system?: Y/N> ')
    metric_system = None
    if question == 'Y' or 'y' or 'yes': 
        metric_system = True
        height = float(input('Enter your height in meters: '))
        weight = float(input('Enter your weight in kilograms: '))
    elif question == 'N' or 'n' or 'no':
        metric_system = False
        height = float(input('Enter your height in feets: '))
        weight = float(input('Enter your weight in pounds: '))
    else:
        'incorrect answer'
        bmi_calc()
    bmi = None
    if metric_system == True:
        bmi = weight / (height ** 2)
    elif metric_system == False:
        bmi = weight / (height ** 2) * 703
    print(f'Your body mass index is {bmi:.2f}')

应该是:

if question == 'Y' or question == 'y' or question == 'yes': 
    metric_system = True
    height = float(input('Enter your height in meters: '))
    weight = float(input('Enter your weight in kilograms: '))
elif question == 'N' or question == 'n' or question == 'no':
    metric_system = False
    height = float(input('Enter your height in feets: '))
    weight = float(input('Enter your weight in pounds: '))
else:
    'incorrect answer'
    bmi_calc()

原因是:if 'y':将始终是True

最新更新