+ 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")

7th Jul 2022, 2:23 AM
Reiss
19 Answers
- 1
Reiss also you can use more pythonic way to chain comparisons: ``` elif 18.5 < bmi <= 25: ``` UPD. For downvoters: https://docs.python.org/3.11/reference/expressions.html#comparisons
8th Jul 2022, 10:03 AM
Евгений
Евгений - avatar
+ 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`.
28th Oct 2022, 7:23 PM
Евгений
Евгений - avatar
+ 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:
7th Jul 2022, 2:44 AM
Brian
Brian - avatar
+ 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: ...
7th Jul 2022, 5:05 AM
Brian
Brian - avatar
+ 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.
7th Jul 2022, 10:03 AM
Reiss
+ 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.
7th Jul 2022, 2:48 AM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Thanks william, Per and Brian. Its worked now.
7th Jul 2022, 2:57 AM
Reiss
+ 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!
28th Oct 2022, 8:00 PM
Samet Akyazı
Samet Akyazı - avatar
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?
7th Jul 2022, 2:48 AM
Reiss
0
Thanks Per, i will correct it now. Thanks for the help.
7th Jul 2022, 2:51 AM
Reiss
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
7th Jul 2022, 2:53 AM
William Owens
William Owens - avatar
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?
7th Jul 2022, 3:06 AM
Reiss
0
Thank you for your help Brian. I will practice some more.
7th Jul 2022, 9:57 AM
Reiss
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.
7th Jul 2022, 10:14 AM
Reiss
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")
7th Jul 2022, 8:43 PM
Ryan
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.
7th Jul 2022, 10:59 PM
Brian
Brian - avatar
0
Thank you, much appreciated
8th Jul 2022, 10:05 AM
Reiss
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")
8th Jul 2022, 8:06 PM
Adebayo Abdul Salam
Adebayo Abdul Salam - avatar
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?
28th Oct 2022, 7:11 PM
Samet Akyazı
Samet Akyazı - avatar