我的第一个if语句始终为True。没有引发错误或异常

时间:2018-11-14 16:09:11

标签: python python-3.x

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

之后一切正常。 感谢您的支持。

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}')

1 个答案:

答案 0 :(得分:0)

应该是:

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