如何检查两个数字之间的int数?

时间:2022-02-09 14:09:03

I'm using 2.3 IDLE and I'm having problems.

我用的是2。3空闲,有问题。

I need to check whether a number is between two other numbers, 10000 and 30000:

我需要检查一个号码是否在另外两个号码之间,10000和30000:

if number >= 10000 and number >= 30000:
    print ("you have to pay 5% taxes")

It's not working too well.

工作得不太好。

6 个解决方案

#1


563  

if 10000 <= number <= 30000:
    pass

#2


46  

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

#3


30  

Your operator is incorrect. Should be if number >= 10000 and number <= 30000:. Additionally, Python has a shorthand for this sort of thing, if 10000 <= number <= 30000:.

你的运营商是不正确的。如果编号>= 10000,编号<= 30000:。此外,Python对这种事情有一个简写,如果10000 <= number <= 30000:。

#4


14  

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特性。

#5


6  

if number >= 10000 and number <= 30000:
    print ("you have to pay 5% taxes")

#6


2  

The trouble with comparisons is that they can be difficult to debug when you put a >= where there should be a <=

比较的问题是,当您将>=放在应该有<=的地方时,它们可能很难调试

#                             v---------- should be <
if number >= 10000 and number >= 30000:
    print ("you have to pay 5% taxes")

Python lets you just write what you mean in words

Python允许你用文字来表达你的意思

if number in xrange(10000, 30001): # ok you have to remember 30000 + 1 here :)

In Python3, you need to use range instead of xrange.

在Python3中,您需要使用range而不是xrange。

edit: People seem to be more concerned with microbench marks and how cool chaining operations. My answer is about defensive (less attack surface for bugs) programming.

编辑:人们似乎更关心微工作台标记和链接操作有多酷。我的回答是关于防御性(bug较少的攻击面)编程。

As a result of a claim in the comments, I've added the micro benchmark here for Python3.5.2

由于评论中有一个声明,我在这里添加了Python3.5.2的微基准

$ python3.5 -m timeit "5 in range(10000, 30000)"
1000000 loops, best of 3: 0.266 usec per loop
$ python3.5 -m timeit "10000 <= 5 < 30000"
10000000 loops, best of 3: 0.0327 usec per loop

If you are worried about performance, you could compute the range once

如果您担心性能,可以计算一次范围

$ python3.5 -m timeit -s "R=range(10000, 30000)" "5 in R"
10000000 loops, best of 3: 0.0551 usec per loop

#1


563  

if 10000 <= number <= 30000:
    pass

#2


46  

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

#3


30  

Your operator is incorrect. Should be if number >= 10000 and number <= 30000:. Additionally, Python has a shorthand for this sort of thing, if 10000 <= number <= 30000:.

你的运营商是不正确的。如果编号>= 10000,编号<= 30000:。此外,Python对这种事情有一个简写,如果10000 <= number <= 30000:。

#4


14  

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特性。

#5


6  

if number >= 10000 and number <= 30000:
    print ("you have to pay 5% taxes")

#6


2  

The trouble with comparisons is that they can be difficult to debug when you put a >= where there should be a <=

比较的问题是,当您将>=放在应该有<=的地方时,它们可能很难调试

#                             v---------- should be <
if number >= 10000 and number >= 30000:
    print ("you have to pay 5% taxes")

Python lets you just write what you mean in words

Python允许你用文字来表达你的意思

if number in xrange(10000, 30001): # ok you have to remember 30000 + 1 here :)

In Python3, you need to use range instead of xrange.

在Python3中,您需要使用range而不是xrange。

edit: People seem to be more concerned with microbench marks and how cool chaining operations. My answer is about defensive (less attack surface for bugs) programming.

编辑:人们似乎更关心微工作台标记和链接操作有多酷。我的回答是关于防御性(bug较少的攻击面)编程。

As a result of a claim in the comments, I've added the micro benchmark here for Python3.5.2

由于评论中有一个声明,我在这里添加了Python3.5.2的微基准

$ python3.5 -m timeit "5 in range(10000, 30000)"
1000000 loops, best of 3: 0.266 usec per loop
$ python3.5 -m timeit "10000 <= 5 < 30000"
10000000 loops, best of 3: 0.0327 usec per loop

If you are worried about performance, you could compute the range once

如果您担心性能,可以计算一次范围

$ python3.5 -m timeit -s "R=range(10000, 30000)" "5 in R"
10000000 loops, best of 3: 0.0551 usec per loop