小编vic*_*how的帖子

函数is_prime - 错误

这是来自codeacademy.com的问题,我正在学习Python.所以我想要的是定义一个检查数字是否为素数的函数.如果是,则返回True.如果不是,则返回False.

这是我的代码:

def is_prime(x):
    lst = []       # empty list to put strings 'False' and 'True'

    for i in range(2,x): # starting at 2 and not including x (number 1 is a divisor of all numbers

        if x <= 2:           # [1] see bellow the explanation
            lst.append('False')
            break

        elif x % i == 0: # if x is divisible by i(number between 2 and not including x)
            lst.append('False')
            break        # break, because we already know x is not prime

        elif …
Run Code Online (Sandbox Code Playgroud)

python math primes numbers

11
推荐指数
1
解决办法
2985
查看次数

标签 统计

math ×1

numbers ×1

primes ×1

python ×1