(Python 3.6)在一定范围内显示素数的函数

时间:2017-10-21 16:04:43

标签: python-3.x list primes

我对编程比较陌生,并且决定在我自己的时间里乱搞并创建我自己的一些基本功能。我相信还有很多其他的变化,但我想让它们成为我自己学习的方式。我目前正在尝试编写一个函数,将某个Max值中的所有素数显示为列表中的限制。但是,我似乎在while循环中遇到了if语句的问题。我不断得到列表索引超出范围,我很困惑为什么。如果有人能向我解释原因,我将非常感激。

    #A function that will hopefully display all primes

def showPrimes(lim):
    """Displays all primes within the range (1, lim]
    """

    fullRange = list(range(2, lim + 1))

    q = 0
    k = 2

    while(q != lim):

        if(fullRange[q] % (q + 2) == 0 and fullRange[q] != (q + 2)):

            fullRange.pop(q) 

        q = q + 1

    return fullRange

0 个答案:

没有答案