Need Help on Python number 26 BMI Calculator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need Help on Python number 26 BMI Calculator

Hello, i need help on my code for this calculator. Here are my two code samples and i think they look fine to me: weight = int(input()) height = float(input()) bmi = wheight/height**2 while bmi >0: if bmi <18.5: print("Underweight") elif bmi >=18.5 and <25: print("Normal") elif bmi >=25 and <30: print("Overweight") else print("Obesity") and weight = int(input()) height = float(input()) bmi = wheight/height**2 while bmi >0: if bmi <18.5: print("Underweight") elif bmi >=18.5 and <25: print("Normal") elif bmi >=25 and <30: print("Overweight") elif bmi >30: print("Obesity") And now im a bit confused about the output of the error message: File "/usercode/file0.py", line 8 if bmi >=18.5 and <25: ^ SyntaxError: invalid syntax Why does it tell me the lesser indicator ("<") is a "invalid syntax"? Thx in advance.

16th Jan 2022, 2:36 PM
Patrick Stubler
7 Answers
+ 4
Why do you use while? Why do you indent more with the elif. It should look more like this. If ....... : do something elif ......... : do something different
16th Jan 2022, 2:45 PM
Paul
Paul - avatar
+ 5
I don't really understand your while loop? You don't need any loop, just if-elif-else conditions Remove the loop, check the indentation and apply Slick 's suggestion
16th Jan 2022, 2:42 PM
Lisa
Lisa - avatar
+ 3
if (bmi >= 18.5) and (bmi < 25): ...
16th Jan 2022, 2:40 PM
Slick
Slick - avatar
+ 3
Nah, look at your indentation as Lisa suggested. You can't have an elif block without an if block in the same scope. Put them all on the same level of indentation.
16th Jan 2022, 2:56 PM
Slick
Slick - avatar
+ 1
Thx for all your help. I'm somewhat sleepy and have done too many errors in the whole syntax. :) Here is my cleaned code who solved everything: 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") elif bmi >30: print("Obesity") Is this clean enough?
16th Jan 2022, 3:01 PM
Patrick Stubler
+ 1
Looks okay. Great you solved it! :)
16th Jan 2022, 3:06 PM
Lisa
Lisa - avatar
0
Hmm tried to clear the Python code from the while loop and now "elif" is pointed out to be a invalid syntax. weight = int(input()) height = float(input()) bmi = wheight/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") Error: File "/usercode/file0.py", line 7 elif (bmi >=18.5) and (bmi <25): ^ SyntaxError: invalid syntax I don't get it. Should i call "print(bmi + "Normal")" and so on? Thx in advance
16th Jan 2022, 2:53 PM
Patrick Stubler