简体   繁体   English

简单的寻找代码有什么问题?

[英]What's the issue with simple prime-finding code?

I\\ma absolutely beginner. 我绝对是初学者。 Just trying to tweak down some basics. 试着调整一些基础知识。 But somehow i cant get this running. 但不知怎的,我不能让这个运行。 This is maybe some stupid oversight or I don't know. 这可能是一些愚蠢的疏忽或我不知道。 Can anyone take a look at this? 谁能看看这个?

import math

def prime(n):
    if n == 1:
        return False
    maxx_d = math.floor(math.sqrt(n))
    for d in range(2 , 1+ maxx_d):
        if n % d == 0:
            return False

        return True
for n in range(1, 21):
    print(n, prime(n))

It's supposed to be show prime numbers but instead this is what get printed: 它应该是显示素数但是这是打印的:

1 False
2 None
3 None
4 False
5 True
6 False
7 True
8 False
9 True
10 False
11 True
12 False
13 True
14 False
15 True
16 False
17 True
18 False
19 True
20 False

Per orhtej2's comment , "Indentation on return True is too deep." 根据orhtej2的评论return True缩进return True太深了。” This will work: 这将有效:

def prime(n):
    if n == 1:
        return False
    maxx_d = math.floor(math.sqrt(n))
    for d in range(2, 1+maxx_d):
        if n % d == 0:
            return False
    return True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM