'How to compare floating point number with two other floating point numbers in python?

When I do the following to compare if the decimal value is between the two values, I get inconsistent results.

x = 1.87

if 1.0 >= x < 2.0:
    print("x: ", x, "; cat: high")
    return 'high'
elif 2.0 >= x < 3.0: 
    print("x: ", x, "; cat: medium")
    return 'medium'
elif 3.0 >= x <= 5.0:
    print("x: ", x, "; cat: low")
    return 'low'
else:
    return None

I get the medium returned as a result. I checked with other values, and none of them matched. I found articles on math.isclose(), but it doesn't help me. How do I solve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source