简体   繁体   English

python代码正在重新启动

[英]python code is restarting

as I'm proceeding slowly with project Euler I started learning python. 随着我对Euler项目的缓慢进行,我开始学习python。 although its a great and simple language i got a bit stuck. 尽管它是一种很棒且简单的语言,但我还是有些卡住。

every code that i wrote and tried to run is automatically restarting. 我编写并尝试运行的每个代码都会自动重新启动。 i think its because of the very very long loop (for example, finding the 10001 prime number), but i cant find out how to fix this issue. 我认为这是因为循环非常长(例如,找到10001质数),但我找不到解决此问题的方法。 can anyone help me, give me a guide line or a tip? 谁能帮我,给我指导或小费?

oh, if its matters im using python 2.7 哦,如果有问题,即时通讯使用python 2.7

thank you! 谢谢!

the code as an example: 该代码作为示例:

count = 0
num = 0
i = 1
def prime(num):
    if num <= 1:
        return False
    if num == 2:
        return True
    else:
        for i in range(3, num):
            if (num % i) == 0:
                return False
                break
        else:
            return True

while (count < 10001):
    if prime(i) == True:
        num == i
        count == count + 1
    i = i + 1

print num    

You just need to change the == to = (twice) in your while loop: 您只需要在while循环中将==更改为= (两次):

while (count < 10001):
    if prime(i) == True:
        num = i
        count = count + 1
    i = i + 1

Then the code runs fine, and prints num as 104729 然后代码运行正常,并输出num104729

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

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