+ 2
BMI Calculator
It says invalid syntax on line 8 but I can’t see what’s wrong. Any help would be much appreciated. weight = float(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: print("Obesity")
19 Answers
+ 3
Samet Akyazı By the order of input. The first input, whatever it be, 1,85 or any other value is assigned to `weight`. The second user input is assigned to `height`.
+ 2
Missing a variable in the conditionals.
elif bmi >= 18.5 and <=25:
... and __?__ <=25:
what should go here?
Likewise, look closely at this line, too.
elif bmi >= 25 and <=30:
+ 2
Reiss also check your logic at the thresholds. What should be the output if BMI is exactly 18.5? 25? 30?
After you resolve that, here is a pro tip:
It is unnecessary to test for both BMI<18.5 and BMI>=18.5. Logically, if one is false, then it is guaranteed that the other is true. You can cut the code down:
if BMI<18.5:
...
elif BMI<25:
...
elif BMI<30:
...
else:
...
+ 2
After reading that again, i see what you’re saying.
If bmi <18.5:
Then it moves on to the next if statement, and so on.
Thanks for showing me the shorter way of doing it.
+ 1
Hi Reiss
You have missed one of the two comparing variables, that you need on each side of the relation a >= b, in the condition.
For example, write:
elif bmi >= 18.5 and bmi <=25:
(instead of :
elif bmi >= 18.5 and <=25:)
Your made the same misstake, even in the next condition.
+ 1
Thanks william, Per and Brian. Its worked now.
+ 1
Евгений and if I would define first the height and in the second line the weight it would put out a wrong bmi. Got it. Thank you!
0
Thanks brian. I see where i went wrong with the second one, but dont have a clue what to do on the first one.
Can you shed any light?
0
Thanks Per, i will correct it now. Thanks for the help.
0
Just one additional comment, remember it is going to stop at the first true statement
if this condition is true:
Do this stuff
elif this condition is true:
Do this stuff
else: **if none of the above are true**
Do this stuff
0
Quick question guys, im very new to this. I’ve been learning for less than a week. How long was it before you felt competent at coding?
0
Thank you for your help Brian.
I will practice some more.
0
Also Brian, i see you have completed quite a few courses. Is Sololearn your your main source for learning material or are you here to just brush up on things?
For me, i knew nothing about coding before starting Sololearn and its my main source of learning material.
0
Hello Guys, please my code is false?
height=float(input())
weight=float(input())
result=(weight/(height**2))
if result<18.5 :
print("Underweight")
elif (result>=18.5 and result<25):
print("Normal")
elif (result>=25 and result< 30):
print("Overweight")
elif (result>=30):
print("Obesity")
or l must do it:
height=float(input())
weight=float(input())
result=(weight/(height**2))
if result<18.5 :
print("Underweight")
elif ( result<25):
print("Normal")
elif ( result< 30):
print("Overweight")
elif (result>=30):
print("Obesity")
0
Ryan it is better to start a new post for questions about your own code.
To answer your question, the logic in either version is fine. It's the input that is wrong. Height and weight are being input in the wrong order.
0
Thank you, much appreciated
0
It like this
weight = int(input())
height = float(input())
calc = weight / (height**2)
if calc < 18.5 :
print("Underweight")
elif calc >= 18.5 and calc < 25 :
print("Normal")
elif calc >= 25 and calc < 30 :
print("Overweight")
else:
print("Obesity")
0
Additional question:
How does everyone know that using
weight = int(input());
height = float(input());
will not be mixed up? How is 1,85m assigned to height and not weight?