【发布时间】:2021-07-08 03:14:14
【问题描述】:
我正在尝试编写一个程序来查找素数,这是我目前的代码
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
我已经检查了与此相关的其他答案,关于在前面的代码行中将变量分配给整数,但我在迄今为止编写的任何代码中都没有看到这一点。我还有什么遗漏的吗?
提前致谢!
【问题讨论】:
-
"TypeError: 'int' object is not callable" 你认为
PrimeList(i)是什么意思? “我已经检查了与此相关的其他答案,关于在前面的代码行中将变量分配给整数”你期望PrimeList = (2)是什么意思? -
PrimeList = (2)应该是PrimeList = [2] -
如果这些不是简单的拼写错误,那么您有一个或多个基本误解应该通过遵循教程来解决,而不是通过尝试提出自己的项目并获得帮助。
标签: python object integer callable