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,506
  • 5
  • 40
  • 52
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,506
  • 5
  • 40
  • 52
Alex
  • 6,610
  • 3
  • 20
  • 38
  • Thank you. I did not encounter such things yet. Appreciate your feedback. – Rokk Ebol Nov 14 '18 at 16:12
  • 2
    also you can use `if question in (['y', 'Y', 'yes']):` –  Nov 14 '18 at 16:13
  • I would use `affirmitive = ["Y", 'y' , 'yes']` `negative = ["N", 'n' , 'no']` `if question in affirmative:` ...etc – krflol Nov 14 '18 at 16:14
  • Take a look at the linked duplicate, [here](https://stackoverflow.com/a/15112149/3279716) to get an idea about multiple conditionals. – Alex Nov 14 '18 at 16:15
  • 1
    `question.lower() in ['y', 'yes']` – ctrl-alt-delor Nov 14 '18 at 16:26
  • Appreciate your comments. I am still struggling to search properly, because I am not a native speaker. Solution with list of answers I don't like because I didn't find that very elegant, but I knew it worked. Thank you. – Rokk Ebol Nov 14 '18 at 16:52