筛选Eratos-thenes Prime数字计划

时间:2018-04-07 19:31:54

标签: python

好的,所以当我在current < n时循环我的代码时,我的编码遇到了麻烦。这是对Eratos-thenes筛选的尝试。如果我删除while循环和intList[0] == current.

,代码会工作一次
intList = []
current = 2

def validNum ( n ):
    if n < 2:
        print ( "Your number must be more than 2. Try again." )
        return 0
    else:
        return 1

def getInput ():
    while 1:
        n = int ( input ( "Please enter a number more than 2: " ) )
        if validNum ( n ):
            return n + 1
            break

n = getInput ()
intList = list ( range ( 2, n ) )

#My poor attempt at a while loop.
while current < n:
    for x in intList:
        if x % current == 0:
            intList.remove ( x )
            #This is an attempt to update the current number to the lowest variable in the list
            intList[0] == current


print ( intList )

任何建议都将不胜感激。

编辑:我现在正在学习我的课程比我想象的要错得多。如果我的用户输入是10,程序应该只在intList中保留2,3,5,7。

1 个答案:

答案 0 :(得分:0)

您的更新使用等于运算符==,而应使用赋值运算符=:

current = intList[0]

这会将current的值设置为intList中的第一个元素。还有一些其他问题需要解决,但这可能会让你朝着正确的方向前进。

相关问题