简体   繁体   English

While循环在Python中无法正常工作

[英]While loop not working properly in Python

So I am trying to find 6th prime number, and the while loop in getPrime is not working properly. 所以我试图找到第六个质数,而getPrime中的while循环无法正常工作。 It is supposed to end when count is bigger than num, but it doesn't. 它应该在count大于num时结束,但不是。 It'd be great if you could help me find out why. 如果您能帮助我找出原因,那就太好了。

import math

def isPrime(num):
    if num == 1:
        return False
    if num % 2 == 0 and num > 2:
        return False
    for i in range(3, int(math.sqrt(num))+1, 2):
        if num % i == 0:
            return False
    return True

def getPrime(num):
    count = 1
    while count < num:
        for i in range(1, 20):
            #print "check"
            if isPrime(i):
                print "prime", i
                count += 1
                print "count", count
            else:
                continue
    print i
    return i

getPrime(6)

In the getPrime() function, when isPrime(i) returns False , you are not incrementing the count variable. getPrime()函数中,当getPrime() isPrime(i)返回False ,您无需增加count变量。 So the while count < num loop gets stuck at that point. 因此, while count < num循环在此时卡住了。

Update: Well, that was my first impression from looking at the code. 更新:嗯,那是我看代码的第一印象。 But then I noticed the nested loop. 但是后来我注意到了嵌套循环。 So I could have misread what was going on. 所以我可能会误读发生了什么。

What I recommend at this point is stepping through the code in a debugger so you can see for yourself what is happening. 我现在建议的是逐步调试程序中的代码,以便您可以自己查看发生了什么。 Do you have a Python debugger available? 您是否有可用的Python调试器?

You can answer almost any question like this yourself if you have a good debugger and know how to use it. 如果您有一个好的调试器并且知道如何使用它,则可以自己回答几乎任何这样的问题。 Then you won't have to wait for your friends on Stack Overflow to take guesses about what's going wrong! 然后,您不必等待Stack Overflow上的朋友来猜测发生了什么问题! :-) :-)

The reason is because your range statement is within the body of the while statement. 原因是因为您的range语句在while语句的主体内。 In another language you might use a do... until statement, but in Python the way to do it is just to add a conditional break statement, eg I have corrected your code to: 在另一种语言中,您可以使用do ...直到语句,但是在Python中,该方法只是添加条件break语句,例如,我已将您的代码更正为:

def getPrime(num):
    count = 1
    for i in range(1, 20):
        if count > num: break
        if isPrime(i):
            highestPrime = i
            count += 1
    return highestPrime

Simplify getPrime, there is no need for a while loop: 简化getPrime,不需要while循环:

def getPrime(num, r):
    gen = (i for i in r if isPrime(i))
    primes = zip(*zip(range(num), gen))[1]
    return primes[num-1] if len(primes) >= num else None

>>> print getPrime(6, xrange(1, 20))
13
>>> print getPrime(6, xrange(100, 500))
127

You're only checking the value of count at the end of the for loop so you will always end up with the full range of 1-20 being tested. 您只需要在for循环的末尾检查count的值,这样就总是可以得到1-20的完整范围。 Why is the range limited to 20? 为什么范围限制为20?

You don't need a for loop at all. 您根本不需要for循环。 Use the while loop to keep finding primes until you have num of them. 使用while循环不断发现的素数,直到你num他们。

Try something like: 尝试类似:

def getPrime(num):
    count = 0 
    i = 1 
    highPrime = None 
    while count < num:
        if isPrime(i):
            count += 1
            highPrime = i 
        i += 1
    return highPrime

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

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