Python bmi challange | Sololearn: Learn to code for FREE!

0

Python bmi challange

Unable to find error in my code. My code is weight=int(input()) height=float(input()) bmi=weight/(height**2) if bmi<18.5: print("Underweight") elif bmi>=18.5 or bmi<25: print("Normal") elif bmi>=25 or bmi<30: print("Overweight") elif bmi>=30: print("Obesity") But somehow answer 2 is incorrect which is I am not able to find why? Plz help.

2/13/2021 2:37:02 PM

Kushal Acharya

6 Answers

New Answer

+4

Kushal Acharya Though you already got an answer, I will try to clarify the problem. That is because you used "or" logical operator on your conditions. For example if bmi is 35, it will print "Normal" instead because of the condition "elif bmi >=25 or bmi < 35" and the bmi >= 25 is True, when it is supposed to be "Obesity". Remember that when using 'or', if either of the condition is True, then the whole condition will become True. TO SOLVE: ---> Just use 'and' instead of 'or'

+2

Kushal Acharya Try this one weight = float(input("")) height = float(input("")) bmi = weight / (height ** 2) if bmi < 18.5: print("Underweight") elif bmi == 18.5 or bmi > 18.5 and bmi < 25.0: print("Normal") elif bmi == 25.0 or bmi > 25.0 and bmi < 30.0: print("Overweight") else: print("Obesity")

+1

Thanks Cyan👍

0

Thanks K.S.S. KARUNARATHNE for the solution but please point out where I was doing mistake I it was for taking float values for both height and weight or == that I was missing.

0

You can try this #your code goes here weight = int(input()); height = float(input()); bmi = weight/float(height*height); if bmi<18.5: print('Underweight') elif bmi>=18.5 and bmi<25: print("Normal") elif bmi>=25 and bmi<30: print('Overweight') else: print('Obesity')

0

bmi = float(input())/float(input())**2 if bmi<18.5:print('Underweight') elif 25>bmi>=18.5:print('Normal') elif 30>bmi>=25:print('OverWeight') elif bmi>=30:print('Obesity')