简体   繁体   English

Python总理检查器

[英]Python prime checker

I recently started learning python (by which I mean, 35 minutes ago at the time of posting...) and I've wrote a few things eg square root generator, factorial generator, Fibonacci number generator, prime checker etc. After I wrote the prime checker, I thought I'd try modifying it so that instead of checking every number in the specified range, it would accept an input and check that one specifically. 我最近开始学习python(我的意思是,发布时是35分钟前……),我写了一些东西,例如平方根生成器,阶乘生成器,斐波那契数生成器,素数检查器等。作为质数检查器,我想我将对其进行修改,以便与其检查指定范围内的每个数字,不如接受一个输入并专门检查一个数字。

DISCLAIMER: If I were better at Python then I would only check numbers up to sqrt(p) and I would add an option to check if it is even and not 2 then it automatically returns that it's not prime but let me walk before I run! 免责声明:如果我比较擅长Python,那么我只会检查不超过sqrt(p)的数字,并且会添加一个选项来检查它是否为偶数而不是2,然后它会自动返回它不是素数,但让我先走一下再运行! :) :)

CODE: 码:

p = input("Enter a potential prime.")

for n in range (2,p):
    if p % n == 0:
        print(p, "equals", n, "x", p//n, "so it isn't a prime number")
        break
else:
    print(p, "is a prime number.")

It works for p = 2 but that's it... 它适用于p = 2,但仅此而已...

NB - Obviously the code is indented accordingly, it's just not formatted properly here. NB-显然,代码已相应缩进,只是此处格式不正确。

I see some errors: You need to convert your user input to an int, plus you need to move the else: clause to beneath your for-loop instead of beneath your if-statement. 我看到一些错误:您需要将用户输入转换为int,另外还需要将else:子句移至for循环下而不是if语句下。 The following code works for what you want: 以下代码可满足您的需求:

p = int(input("Enter a potential prime."))
for n in range (2,p):
    if p % n == 0:
        print(p, "equals", n, "x", p//n, "so it isn't a prime number")
        break
else:
    print(p, "is a prime number.")

Yes, this is correct - the else: is NOT part of the if-statement, it is part of your for-loop. 是的,这是正确的else:不是if语句的一部分,它是for循环的一部分。 This syntax means that if your for-loop runs to a break, then it'll break as normally. 这种语法意味着,如果您的for循环运行到中断,那么它将正常中断。 If there is no break, then the else: clause will be executed. 如果没有中断,则将执行else:子句。 Thus, it'll do the basic trial division, and if the number passes the test, it'll print "is a prime number" only once. 因此,它将进行基本的试验划分,并且如果该数字通过测试,则只会打印一次“是质数”。 The code you posted will print "is a prime number" for every iteration of your loop. 您发布的代码将为循环的每次迭代打印“是素数”。


Edit: Try the following code for your followup question. 编辑:尝试下面的代码来解决您的后续问题。

def primeChecker():
    # Function that keeps prompting for input, and checks if it's prime. Enter "quit"
    # to exit:
    user_input = input("Enter a potential prime (or type 'quit' to exit): ")
    if user_input == 'quit':
        return
    else:
        p = int(user_input)

    # Trial division algorithm:
    for n in range (2,p):
        if p % n == 0:
            print(p, "equals", n, "x", p//n, "so it isn't a prime number")
            break
    else:
        print(p, "is a prime number.")

    # Recursive function call:
    primeChecker()

# Start by calling the main function:   
primeChecker()

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

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