0

Bmi

This is my bmi calculator let me know what you think? #your code goes here weight=int(input()) height=float(input()) i=weight/(height**2) if i <= 18.5: print ('Underweight') elif i<=25 and i>18.5: print ('Normal') elif i<=30 and i>25: print ("Overweight") elif i>=30: print ("Obesity")

21st Jan 2023, 5:19 PM
james ewell
james ewell - avatar
8 Answers
+ 3
You code looks good. You can write an else statement at the end instead of elif as it does the same thing (meaning you get the same output) but it's shorter: ... else: print("Obesity")
21st Jan 2023, 5:42 PM
blank
+ 3
I see what yoh mean the last line should be Elif i>30
21st Jan 2023, 6:18 PM
james ewell
james ewell - avatar
+ 2
i<=30 and i>=30 — in 2 different statements do not make sense. If the answer is 30, which one will be printed?
21st Jan 2023, 6:16 PM
Lamron
Lamron - avatar
+ 1
Hi james ewell you could use for range testing the annotation: if 18.5 < i <= 25:
21st Jan 2023, 6:39 PM
JuZip
+ 1
james ewell you can reduce the logic. For example, when i<=18 is false, then i>18 must be true so there is no need to explicitly test for that. Simply do one conditional at each threshold. Also, the task indicates that if it is equal to 18.5 then bmi is 'Normal', not 'Underweight'. And the rules are likewise at the other thresholds. The conditonals need correction as below: #your code goes here weight=int(input()) height=float(input()) i=weight/(height**2) if i<18.5: print ('Underweight') elif i<25: print ('Normal') elif i<30: print ("Overweight") else: print ("Obesity")
22nd Jan 2023, 1:56 AM
Brian
Brian - avatar
+ 1
Hey james ewell it won't enter because it's elif. In case you code another if your thought would be right. Elif is entered like else, when the if statement results false and tests a new condition. Else would be entered in any case that none of the if/elif is true.
22nd Jan 2023, 6:13 PM
JuZip
+ 1
Ah okay that makes sense, thank you for you comment!!
22nd Jan 2023, 6:17 PM
james ewell
james ewell - avatar
0
Brain, correct me if im wrong. But if i do elif i<25 let say the number was 17 it would be true for all statments that its less the 25 and 30 so wouldnt it print underweight,normal,and overwieght?
22nd Jan 2023, 6:05 PM
james ewell
james ewell - avatar