2 comparison operators in an if statement

I have tried varieties of code to use 2 comparison operators in this if statement. I’m just stuck.

Weight = float(input())
Height = float(input())
BMI = Weight / (Height**2)
if BMI < 18.5:
  print("Underweight")
  if BMI >= 18.5 < 25:
    print("Normal")
    if BMI >= 25 < 30:
      print("Overweight")
      if BMI >= 30:
        print("Obesity")

Not sure what language this is but generally you would combine them with a logical AND or logical OR:

if BMI >= 18.5 AND BMI < 25:

Replace AND with whatever operator the language uses for logical AND.

The assignment operator always performs calculations from right side of the equation.

So in your code if BMI>= 25 < 30, it compares the numbers 25 < 30 initially, which returns a boolean as false.

Then BMI>= false is compared which doesnt make sense.

Hence it is better to split the code with logical operators as @bbsmooth suggested.

Happy Coding!

Yea as I said I tried many varieties resulting in syntax error on the 6th line where the double comparison operators take place.

Does the language have a logical AND operator? If so, have you tried what I suggested. It would also probably help to know what language this is.

Yea I was just gonna type python. Tried the and logic. Still syntax error on line6. Tried elif or. Nothing seems to work

What is your new code? We can’t help with code you don’t show.

Your indentation is strange. You shouldn’t be indentating each if more and more like that.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I’ve honestly never programmed in python in my life and I was able to modify what you have using the logical “and” operator and it worked. I would give it another try.

This is how they explain to do the if statement.
“Indentation is used to define the level of nesting.”
I don’t have any “new” code. I have tried, before coming to this forum, and, or, elif, parenthesis around comparison operators, separate line with and, or, and just entering the variable <

We can’t see any of these attempts and can’t comment on them.


Your code code for making two logical comparisons is wrong. You need to use a logical and. Show us code with a logical and and we can help you fix it.


You are excessively indenting. If you place one if statement inside of another, you will only execute the body of the second if statement when both are true. That will never happen with the comparisons you are making. Show us your code with an if-else on the same level and we can help you fix it.


Weight = float(input())
Height = float(input())
BMI = Weight / (Height**2)
if BMI < 18.5:
  print("Underweight")
  if BMI >= 18.5 and < 25:
    print("Normal")
    if BMI >= 25 and < 30:
      print("Overweight")
      if BMI >= 30:
        print("Obesity")

Still not working

The logical and needs to have a complete comparison on each side. < 25 is not a complete logical comparison.

Also… You are excessively indenting. None of your code with the higher indentation level will every be able to run because the conditions are exclusive.

In the tutorial and quote:

“So what are the other comparison operators? Well, we’ve got: equal to ==, not equal to !=, greater than >, smaller than <, greater or equal to >=, smaller or equal to <=.”
So I don’t understand what you mean by complete comparison operator.
Do you mean adding the variable?

If I walk up up to you and say ‘less than twenty-five’, will you have any clue what I mean? What is less than 25? You have not told Python what you want to compare to 25.

In order to use a comparison operator you need a complete expression with 1) a value on the left 2) a comparison operator in the middle and 3) a value on the right.

In order to make two comparisons at once, you need two complete comparison expressions separated by an and.

Yea I tried that b4 and it still didn’t work. I guess I didn’t do it exactly correct. Could have been the nesting. But by example that is the way they show you how to do it. Believe me, I tried just nesting the print function and still couldn’t get it to work. I guess it just wasn’t all done at the same time. Thanks guys. I finally got it.

Weight = float(input())
Height = float(input())
BMI = Weight / (Height**2)
if BMI < 18.5:
  print("Underweight")
if BMI >= 18.5 and BMI < 25:
  print("Normal")
if BMI >= 25 and BMI < 30:
  print("Overweight")
if BMI >= 30:
  print("Obesity")
1 Like

Code is finiky. Small syntax changes make all the difference. Good job getting in working.

Pssshh. I think anyone that has done any code realizes very soon into their endeavors. Lol

Python has can chain comparisons, but the order needs to even more make kind-of math sense in the chain.

x = 15
x >= 2 < 10  # True
2 <= x < 10  # False

Those are equivalents to:

x >= 2 and 2 < 10  # True and True -> True
2 <= x and x < 10  # True and False -> False
1 Like

Your code is working nicely with a slight modification. You are excessively indenting. Try this code and find out your errors.