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 Réponses
+ 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(