0

python for beginners code project BMI calculator

Hi, I was doing the code project, I have written this code: weight = int(input()) height = float(input()) imc = weight/(height**2) if imc<18.5: print("Underweight") elif (imc>=18.5) and (imc<=24.9): print("Normal") elif (imc>24.9) and (imc<=29.9): print("Overweight") elif imc>29.9: print("Obesity") and in the fourth test case it tells me that is wrong, it's an error or I have done it wrong?

8th Aug 2021, 6:07 PM
PabloB04
PabloB04 - avatar
4 Answers
+ 4
change every 24.9 to 25 and 29.9 to 30 and try it again
8th Aug 2021, 6:43 PM
Mehrdad Sh
Mehrdad Sh - avatar
+ 3
u should use 25 instead of 24.9 and 30 instead of 29.9 and change equal to greater to greater for second conditions might be 24.95 that is normal and u print overweight
8th Aug 2021, 6:28 PM
Mehrdad Sh
Mehrdad Sh - avatar
0
Mehrdad Sh like this? weight = int(input()) height = float(input()) imc = weight/(height**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")
8th Aug 2021, 6:35 PM
PabloB04
PabloB04 - avatar
0
I know this post is a bit old, but there's a simpler way to think about it. If you look at the "Normal" range, it must be >= 18.5, but all numbers in existence that aren't >= 18.5 are instead < 18.5. You already tested for that, and then said "else if", so you don't need it again.... if imc < 18.5: ... elif imc < 25: ... elif imc < 30: ... else: print("Obesity") Adding >= 18.5 in the else condition of < 18.5 is unnecessary.
3rd Sep 2021, 1:18 AM
Kyle Alm
Kyle Alm - avatar