+ 1

Help, pls (BMI calculator)

Can you pls fix this crap or just idk explain how w = int(input()) h = float(input()) h **2 = h h * w = x if x <18.5: print ('underweight') else: if x >= 18.5: print ('Normal') else: if x >= 24.9: print('overweight') else: if x >= 29.9 print ('obese')

13th Dec 2021, 8:06 PM
Ash Sharif
Ash Sharif - avatar
6 Antworten
+ 2
weight = float(input()) height = float(input()) BMI = weight / (height ** 2) if BMI < 18.5: print('Underweight') elif 18.5 <= BMI < 25: print("Normal") elif 25 <= BMI < 30: print("Overweight") elif BMI > 30: print('Obesity')
13th Dec 2021, 8:29 PM
Wellsie
Wellsie - avatar
+ 2
In your code if the BMI >= 18.5, it prints Normal, also if it is larger than 24.9 or 29.9, because it is the first if that is true, the else statements after it won't happen.
13th Dec 2021, 9:35 PM
Paul
Paul - avatar
+ 1
More: - Why is weight an int, instead of float? A float is more flexible, without additional complexity. - The assignments are reversed. For example: 'h**2 = h' tries to put h into h**2, what doesn't make sense. If needed, review the assignment lesson. - Instead of nested if statements, use if... elif... elif... else blocks, as Wellsie pointed out. This guarantees one and only one output. If needed, review if, else, and elif statements lesson. - Correct indentation. This is critical in Python. If needed, review indentation/blocks lesson. - Think your logic all the way. Run the code in your mind, with values inside ranges, including edge cases. This teaches a lot!
13th Dec 2021, 10:00 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Thank you guys
14th Dec 2021, 2:10 PM
Ash Sharif
Ash Sharif - avatar
0
What you did wrong was: ●use multiple else statements which is incorrect , you can only use it once at the end . Correction : use elif ●indent the else statements , only indent the else expression not the statement itself . ●the values aren't matched with The values that are given in the task. Hope this helped 🧡
13th Dec 2021, 8:32 PM
Wellsie
Wellsie - avatar