0
if totalbmi <= 19:
    print('Your bmi is {}, you are underweight'.format(totalbmi))
elif totalbmi >= 20 or totalbmi <= 28:
    print('Your bmi is {} you are normal weight'.format(totalbmi))
else:
    totalbmi >= 29
    print('your bmi is {} you are overweight'.format(totalbmi))

is returning: Your bmi is 55.4016620498615 you are normal weight.

Sorry for this obviously dumb beginner question, but I am a dumb beginner and everything seems logically correct to me. If totalbmi is a number greater than 20, or less than 28 it returns "Normal weight" as it's supposed to. If totalbmi is a number less than or equal to 19 it returns "underweight" again as it's supposed to. But anything >= 29 always seems to loop back into "You are normal weight" and is not returning "You are overweight" ... as it should?

Thanks in advance for your help

4 Answers 4

2

Should be elif totalbmi >= 20 and totalbmi <= 28:

Sign up to request clarification or add additional context in comments.

2 Comments

AND instead of OR! As it was Or it was picking every number greater than or equal to 20 and assigning it to "You are normal weight" and basically everything after that 'or' was extraneous to the block. Thanks so much for the help and answering my dumb question.
@JGiacalone repeating interval bounds in different conditions is a bad idea. Check how your code works with boundary values +- ε. Given that your output example is 55.4016620498615, I assume you are working with floats and should handle this case.
1

You can use the logic of if-elif-else execution: Python runs from top to bottom. That means it checks the condition from top to bottom and breaks when conditions are met. If that is the case then this will do:

if totalbmi > 28:
    print('your bmi is {} you are overweight'.format(totalbmi))
elif totalbmi > 19:
    print('Your bmi is {:} you are normal weight'.format(totalbmi))
else:
    print('Your bmi is {}, you are underweight'.format(totalbmi))

Comments

0

Agree with Jonathan, but best is still:

elif 20 <= totalbmi <= 28:

Demo:

if totalbmi <= 19:
    print('Your bmi is {}, you are underweight'.format(totalbmi))
elif 20 <= totalbmi <= 28:
    print('Your bmi is {} you are normal weight'.format(totalbmi))
elif totalbmi >= 29:
    print('your bmi is {} you are overweight'.format(totalbmi))

Other solution:

elif int(totalbmi) in range(20,29):

Demo:

if totalbmi <= 19:
    print('Your bmi is {}, you are underweight'.format(totalbmi))
elif int(totalbmi) in range(20,29):
    print('Your bmi is {} you are normal weight'.format(totalbmi))
elif totalbmi >= 29:
    print('your bmi is {} you are overweight'.format(totalbmi))

2 Comments

Also there are some values not in range such as 19.5 or 28.5
@soon Ok only thing i can do is now int
0

Agreed or condition checks whether any of the condition is true. Let say totalbmi=66 so on reaching elif totalbmi>=66 so condition true thats why it was printing the print statement of elif and never reaching the else part.

On using and totalbmi>=66 (condition true) and totalbmi<=66 (condition false) so it jumps to else part and print that statement

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.