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")
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.
+ 2
Try using the search bar first.
https://www.sololearn.com/Discuss/3025109/?ref=app
+ 2
Thank you so much!!!
+ 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
+ 1
Oh! That's right! You help me a lot!!!!