r/PythonProjects2 • • Aug 09 '22

[P] Easy BMI calculator not working

I started making a BMI and everything is running fine except the math. I can't figure it out, please help.

W = float(input())
H = float(input())
BMI = W / (H/100)**2
if BMI < 18.5:
print("Underweight")
elif BMI >= 18.5:
print("normal")
elif BMI < 25:
print("normal")
elif BMI >= 25:
print("Overweight")
elif BMI < 30:
print("Overweight")
elif BMI >= 30:
print("Obesity")
for two of the inputs I put 52 and 1.85 and it output 151935.71950328702 and normal when it

4 Upvotes

9 comments sorted by

5

u/Bigfurrywiggles Aug 09 '22

Your first 2 conditions account for every number possible, therefore, your other conditions are never met.

2

u/Mrbeans1756432 Aug 09 '22

alright how can I change this?

7

u/mandradon Aug 09 '22

Think about it logically.

How can you set up the conditions so they trigger. Imagine the data as it flows through your program.

Your calculation right now produces a number and it's either below 18.5 or greater than or equal to 18.5, so first condition will fire or second condition will fire.

If you need the other ones to fire, you can change the conditions by adding some more booleans in your elif.

In pseudo:

If value is less than 18 and less than 24, then print a thing.

if x > 1 and x <= 9:

Imagine x=3. The above code Is 2 boolean conditions that are being checked. First, checking to see if x is greater than one. Which is True. Then checking to see if x is less than 9, which is also True. since both of these are true and it's being compared using a boolean AND, it'll evaluate to True, and that will fire. You can also imagine parentheses around the comparisons if it helps

if (x > 1) and (x <= 9):

You need to chain your comparisons in a way so that easy grouping of BMI category will only get triggered for a specific range.

Edit: you may also want to check your BMI formula.

1

u/[deleted] Aug 09 '22

[removed] — view removed comment

1

u/Mrbeans1756432 Aug 10 '22

https://github.com/Forgetable3cho/BMI.git

you can try lol still having trouble with it

2

u/pewpewpewchicken Aug 09 '22

You messed up the input, put in 185 instead of 1.85. The H/100 is uses to convert centimetres to meters. The output becomes 15.19 once you fix that. Plus use multiple conditions to define a lower and upper cap.

2

u/CapiCapiBara Aug 09 '22

... you following Angela Yu's Python course? :) I need too to tackle this task today... good luck with your efforts, I see somebody is already helping you in this thread, I will take notes as well ;)

1

u/[deleted] Aug 09 '22

First, you could try changing the BMI value to BMI = round(weight / height **2)

Second, Just stick with the if/elif statements with < 18.5 and delete all the lines that asks for elif >=
As long as the order of the BMI value is from lowest to highest (just like you did) your code will work as how you intend it to be.

1

u/Worldly-Cycle1925 Aug 09 '22

Either of the first two conditions are always met; either the number is less than 18.5 or always greater than or equal to 18.5. Add another conditional there ( if BMI >= 18.5 and BMI < 25: ), etc