+ 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')
6 Réponses
+ 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')
+ 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.
+ 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!
+ 1
https://www.sololearn.com/discuss/2684939/?ref=app
https://www.sololearn.com/discuss/2940297/?ref=app
https://www.sololearn.com/discuss/2873133/?ref=app
https://www.sololearn.com/discuss/2789472/?ref=app
https://www.sololearn.com/discuss/2834561/?ref=app
https://www.sololearn.com/discuss/2697189/?ref=app
https://www.sololearn.com/discuss/2706757/?ref=app
https://www.sololearn.com/discuss/2922944/?ref=app
https://www.sololearn.com/discuss/2735780/?ref=app
https://www.sololearn.com/discuss/2885794/?ref=app
https://www.sololearn.com/discuss/2817626/?ref=app
https://www.sololearn.com/discuss/2687547/?ref=app
https://www.sololearn.com/discuss/2686011/?ref=app
https://www.sololearn.com/discuss/2776665/?ref=app
https://www.sololearn.com/discuss/2907384/?ref=app
https://www.sololearn.com/discuss/2788765/?ref=app
https://www.sololearn.com/discuss/2755773/?ref=app
https://www.sololearn.com/discuss/2686226/?ref=app
https://www.sololearn.com/discuss/2697171/?ref=app
https://www.sololearn.com/discuss/2797968/?ref=app
http
+ 1
Thank you guys
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 🧡