本文翻译自:Determine Whether Integer Is Between Two Other Integers?
How do I determine whether a given integer is between two other integers (eg greater than/equal to 10000
and less than/equal to 30000
)? 如何确定给定的整数是否在其他两个整数之间(例如,大于/等于10000
且小于/等于30000
)?
I'm using 2.3 IDLE and what I've attempted so far is not working: 我正在使用2.3 IDLE,到目前为止,我一直没有尝试:
if number >= 10000 and number >= 30000:
print ("you have to pay 5% taxes")
#1楼
参考:https://stackoom.com/question/vBTD/确定整数是否在其他两个整数之间
#2楼
if number >= 10000 and number <= 30000:
print ("you have to pay 5% taxes")
#3楼
Your operator is incorrect. 您的操作员不正确。 Should be if number >= 10000 and number <= 30000:
. if number >= 10000 and number <= 30000:
。 Additionally, Python has a shorthand for this sort of thing, if 10000 <= number <= 30000:
. 另外, if 10000 <= number <= 30000:
,Python就是这种事情的简写形式。
#4楼
if 10000 <= number <= 30000:
pass
#5楼
Your code snippet, 您的代码段,
if number >= 10000 and number >= 30000:
print ("you have to pay 5% taxes")
actually checks if number is larger than both 10000 and 30000. 实际检查数字是否同时大于10000和30000。
Assuming you want to check that the number is in the range 10000 - 30000, you could use the Python interval comparison: 假设您要检查数字范围在10000-30000之间,可以使用Python间隔比较:
if 10000 <= number <= 30000:
print ("you have to pay 5% taxes")
This Python feature is further described in the Python documentation . Python文档中进一步描述了此Python功能。
#6楼
>>> r = range(1, 4)
>>> 1 in r
True
>>> 2 in r
True
>>> 3 in r
True
>>> 4 in r
False
>>> 5 in r
False
>>> 0 in r
False