Python Forum

Full Version: Issue with program not passing to other elif condition
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello

I am new to this forum and programming in general.

I am trying to create BMI calculator. the issue is that no matter what I do the program stops only on the first elif statement (>=18.5 to >=24.9), even if the BMI is higher


code:
print('enter you weight kg')
weight=float(input())
print('enter you height meter')
height=float(input())
BMI=(weight/height**2)

print(BMI)


if BMI<=18.4:                                   
    print('You are underweight')
elif BMI>=18.5 or BMI<=24.9:
    print(' You have Normal weight')
elif BMI >=25 or BMI<=29.9:
    print('You are overweight')
elif BMI>=30 or BMI<=34.9:
    print('You are moderately obese')
elif BMI >=35 or BMI<=39.9:
    print('You are severely obese')
elif BMI>=40:
    print('You are very severely obese')
Thank you
Change all the or's to and

Please use the tag's when posting code.
if BMI<=18.4:
print('You are underweight')
elif BMI>=18.5 or BMI<=24.9:  # These should be 'and', not 'or'
print(' You have Normal weight')
elif BMI >=25 or BMI<=29.9: # And if I am 24.937?  No.  <= 18.4; >18.4 and < 25; >= 25 and < 30 
print('You are overweight')
elif BMI>=30 or BMI<=34.9:
print('You are moderately obese')
elif BMI >=35 or BMI<=39.9:
print('You are severely obese')
elif BMI>=40:
print('You are very severely obese')
In Python, you can do
if 30 <= BMI < 35:
    ...
Thank you for pointing that out. I'm still learning Python. Key here is the code as shown has these huge gaps, such as between 2.9 and 3; ranges must be open at one end and closed at the other, and continuous in a construct such as the OP showed. No gaps in the range are permitted.
"or" is wrong. Every number is less than 25 OR greater than 18.4. You could use "and", but there is no reason for this as it just adds code, complexity and increases the chance for errors such as "I have a bmi of 18.45, what am I? According to the original post you are nothing as this number falls in the gap between underweight and normal weight.

Instead of specifying ranges EXPLICITELY you should do so IMPLICITELY. If you write the comparisons in increasing or decreasing order you don't have to specify both ends of the range. This code has no gaps. Every possible BMI value is handled.
if BMI <= 18.4:                                   
    print('You are underweight')
elif BMI < 25:
    print(' You have Normal weight')
elif BMI < 30:
    print('You are overweight')
elif BMI < 35:
    print('You are moderately obese')
elif BMI < 40:
    print('You are severely obese')
else
    print('You are very severely obese')
Thank you all for the help