Chained comparison number range in Python

前端 未结 3 1265
醉话见心
醉话见心 2021-01-14 04:25

I have the following function:

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

I want this to say false if the number is not within

3条回答
  •  温柔的废话
    2021-01-14 04:55

    Use this:

    1 <= number <= 5
    

    From docs:

    x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

    Your (incorrect)expression is actually equivalent to:

    number >=5 and number >= 1
    

    So, it is going to be True for any number between 1 to infinity:

提交回复
热议问题