I have the following function:
def InRange(number):
return 5 <= number >= 1
I want this to say false if the number is not within
Alternatively you can do (it seemed appropriate based on the function's name):
def InRange(number):
return number in range(1, 6)
For large numbers you should use:
def InRange(number):
return number in xrange(1, 10000000)
Use this:
1 <= number <= 5
From docs:
x < y <= z
is equivalent tox < y
andy <= z
, except thaty
is evaluated only once (but in both casesz
is not evaluated at all whenx < 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:
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