hello! i am creating BMI calculator in python but it doesn't pass test 2. can anyone help me? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

hello! i am creating BMI calculator in python but it doesn't pass test 2. can anyone help me?

altura= float(input()) peso= float(input ()) IMC = peso / altura **2 if IMC < 18.5: print ("Underweight") elif IMC >= 18.5 and IMC <= 24.9: print("Normal") elif IMC >= 25 and IMC <= 29.9: print ("Overweight") elif IMC >= 30: print ("Obesity")

15th May 2022, 5:42 AM
Cristopher Evedorio Padilla Flores
Cristopher Evedorio Padilla Flores - avatar
5 Antworten
+ 2
Your inputs are reversed. It should be peso, then altura. The logic gives wrong results in some cases. For instance, if IMC is 24.95 the output should be "Normal", but your program gives no output. Simplify and correct the logic like this: if IMC < 18.5: print ("Underweight") elif IMC < 25: print("Normal") elif IMC < 30: print ("Overweight") else: print ("Obesity") As you can see, there is no need to test >= 18.5 after you already tested < 18.5 and found it is false.
15th May 2022, 9:44 AM
Brian
Brian - avatar
+ 2
15th May 2022, 5:47 AM
Chris Coder
Chris Coder - avatar
+ 2
Thank you so much!!!
15th May 2022, 6:40 AM
Cristopher Evedorio Padilla Flores
Cristopher Evedorio Padilla Flores - avatar
+ 1
Another tip is when using comparison operators in Python, You can do it without using and , like how it is written in maths. 18.5 <= IMC < 25
15th May 2022, 8:54 AM
Amaani
Amaani - avatar
+ 1
Oh! That's right! You help me a lot!!!!
15th May 2022, 7:50 PM
Cristopher Evedorio Padilla Flores
Cristopher Evedorio Padilla Flores - avatar