1

Looks like my first if statement doesn't work and always goes yes. I would like to provide choice in my calculation program, whether user uses metric system or not.

Anything works fine after that. Appreciate your support.

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}')
ctrl-alt-delor
  • 7,028
  • 5
  • 35
  • 49
Rokk Ebol
  • 11
  • 1
  • Your program would produce an error in some languages, saying that 'y' and 'yes' are not Boolean values, so can not be used in an `or`. However python converts both to True. Because `something or True or True` is True, the if is always done. – ctrl-alt-delor Nov 14 '18 at 16:25
  • Choosing good name for variables is very important: `y` is not a question. It is an answer. and Python does not do tail-call optimisation, so don't use recursion to loop. – ctrl-alt-delor Nov 14 '18 at 16:28

1 Answers1

0

It should be:

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()

The reason is because: if 'y': will always be True

ctrl-alt-delor
  • 7,028
  • 5
  • 35
  • 49
Alex
  • 5,950
  • 3
  • 18
  • 36