Search code examples
pythoncomparisonchained

Chained comparison number range in Python


I have the following function:

def InRange(number):
    return 5 <= number >= 1

I want this to say false if the number is not within the range of 1 to 5 using a chain comparison, but cannot seem to get this right.

Any suggestions?


Solution

  • You want it like this:

    def InRange(number):
        return 1 <= number <= 5
    

    Note that you could also do:

    def InRange(number):
        return 0 < number < 6