查找 Prime 时收到“TypeError: 'int' object is not callable”错误?

时间:2021-04-13 04:00:38

标签: python object integer callable

我正在尝试编写一个程序来查找素数,这是我目前的代码

import math
import numpy 
PrimeList = (2)    #starting prime list at 2
for numbers in range(3,10001):    #beginning loop for primes from 3 to 10000
    i = 0
    PrimeList_flag = True
    while PrimeList(i) <= math.floor(math.sqrt(numbers)):    # checks number divisibility by any prime less than or equal to sqrt of number
        if numbers % prime(i)==0:
            PrimeList_flag = False    # sets flag to true if the last note is true
            break    #loop breaker
        i += 1   # for i greater than or equal to 1
    if PrimeList_flag ==True: 
        PrimeList.append(num)
print(PrimeList)

返回此错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-38-304e050286f5> in <module>
      5     j = 0
      6     PrimeList_flag = True
----> 7     while PrimeList(j) <= math.floor(math.sqrt(numbers)):    # checks number divisibility by any prime less than or equal to sqrt of number
      8         if numbers % prime(j)==0:
      9             PrimeList_flag = False    # sets flag to true if the last note is true

TypeError: 'int' object is not callable

我已经在前面的代码行中检查了与将变量分配给整数相关的其他答案,但我在迄今为止编写的任何代码中都没有看到这一点。还有什么我遗漏的吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

首先,python 中的列表是用方括号声明的——你拥有的是一个简单的整数。

其次,对于您的特定问题的答案,您必须使用方括号进行索引。括号用于调用可调用对象(函数)。所以你需要使用

PrimeList[i]

另外,prime 是从哪里来的?那是你导入的函数吗?