Python无法识别1000以上的数字

时间:2017-02-05 09:01:12

标签: python

magicnumber = 2000 ;

for x in range(10000):
    if x is magicnumber:
        print(x,"Is the Magic Number")
        break

我需要帮助。

3 个答案:

答案 0 :(得分:4)

您需要将is替换为==。您需要阅读本文以获得更多理解:Is there a difference between `==` and `is` in Python?

magicnumber = 2000 ;

for x in range(10000):
    if x == magicnumber:
        print(x,"Is the Magic Number")
        break

输出:

(2000, 'Is the Magic Number')

答案 1 :(得分:1)

if x is magicnumber:

相同
if x is 2000:

返回false,因此永远不会满足该条件

if x == magicnumber:

正是你要找的......

答案 2 :(得分:1)

magicnumber = 2000

for x in range(10000):
    if x == magicnumber:
        print(x,"Is the Magic Number")
        break