0
Bmi calculator
Please help me. Can't understand what's wrong. weight = float(input()) height = float(input()) bmi = weight/height**2 if bmi < 18.5: print ('Underweight') if bmi >= 18.5: print ('Normal') if bmi >= 25.0: print ('Overweight') if bmi >= 30.0: print ('Obesity')
4 Answers
+ 1
G'day Ruslan Mustafaev I'm sure you have learned that Python uses indentation.
Your second đifđ is indented, that tells Python to only run that code when the previous if is true.
â
if bmi <18.5:
#indented lines run when the condition is true
âif bmi >= 18.5: #indented. Never is true because you have only allowed value less than 18.5
Also, you want to tear off values as you check them. Start by checking the smaller values, then the next smallest, etc etc.
â
if bmi <18.5:
#catches all less than 18.5, including zero & negatives.
#next check needs to be the same indentation as the first if
â
elif bmi <25.0:
#catches any remaining value below 25.0, note that the values below 18.5 have already been dealt with, no double up here. You don't have to use đ>= 18.5 and <25.0đ because you have already torn off those values.
â
elif bmi <30.0:
#etc etc
â
else:
#else takes the remaining values that didn't fit into any of the other ifs.
+ 2
Suppose a person has a bmi of 20. So bmi < 18.5 will be false, nothing will be printed.
+ 1
HungryTradie Thank you so much! Your answer helped me to understand at least 2 mistakes!
0
Thanks! Forgot that should be 'else' there(