Bmi calculator | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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')

28th Apr 2022, 7:38 PM
Ruslan Mustafaev
Ruslan Mustafaev - avatar
4 Respostas
+ 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.
29th Apr 2022, 5:30 AM
HungryTradie
HungryTradie - avatar
+ 2
Suppose a person has a bmi of 20. So bmi < 18.5 will be false, nothing will be printed.
28th Apr 2022, 7:54 PM
Lisa
Lisa - avatar
+ 1
HungryTradie Thank you so much! Your answer helped me to understand at least 2 mistakes!
29th Apr 2022, 9:51 AM
Ruslan Mustafaev
Ruslan Mustafaev - avatar
0
Thanks! Forgot that should be 'else' there(
28th Apr 2022, 8:20 PM
Ruslan Mustafaev
Ruslan Mustafaev - avatar