(Beginner Question) BMI Calculator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

(Beginner Question) BMI Calculator

Hello there. I have some trouble identifying whats wrong. If I run this code I will get the error message that there is a Syntax error in line 2 but I can't find it. weight = int(input()) height = float(input()) BMI = (weight/height**2) if BMI < 18.5: print("Underweight") elif BMI>=18.5 and <25: print("Normal") elif BMI>=25 and <30: print("Overweight") else BMI <30: print("Obesity") I am really glad for any ideas. I also tried to change the weight input to float. But if I do it like this weight = float(input()) I get the Syntax errormessage for line 1.

30th Aug 2022, 12:46 PM
Lukas Mußhoff
Lukas Mußhoff - avatar
6 Answers
+ 2
Compare 1) elif BMI >= 18.5 and BMI < 25 to 2) elif BMI >= 18.5 and < 25 1) is most likely what you want to do.
30th Aug 2022, 8:59 PM
Lisa
Lisa - avatar
+ 1
I don't see an issue in line 1 or 2. Could you copy your original code in a script on sololearn playground? Go to Code section, click +, select Python, insert your code, save. Come back to this thread, click +, Insert Code, sort for My Code Bits, select your code. Also check on your if-elif-else-conditions: – Note the usage of BMI: elif BMI >= 18.5 and BMI < 25 – same applies for the 2nd elif – else does not take any condition, it is just the "remainder" when no condition else is met. Simple remove the condition from else (Also elif BMI < 30 wouldn't make much sense regarding the previous condition.)
30th Aug 2022, 1:07 PM
Lisa
Lisa - avatar
0
I just editet the Code to this weight = int(input()) height = float(input()) BMI = (weight/height**2) if BMI < 18.5: print("Underweight") elif BMI>=18.5 and BMI<25: print("Normal") elif BMI>=25 and BMI<30: print("Overweight") else: print("Obesity") Now its working. I honestly do not fully get it, but it worked.
30th Aug 2022, 1:03 PM
Lukas Mußhoff
Lukas Mußhoff - avatar
0
Lisa Thank you very much for your answer. Do you mean what Sreeju just did? I understand the problem with the else condition. I changed it in the updated code. I do not understand what you mean with the elif condition. Can you explain?
30th Aug 2022, 8:44 PM
Lukas Mußhoff
Lukas Mußhoff - avatar
0
Lisa Oh yeah, thank you. Now I can see it. I also adressed this in the updated code. Thank you very much for your time.
30th Aug 2022, 9:06 PM
Lukas Mußhoff
Lukas Mußhoff - avatar