1

I have recently decided to learn basic programming, and am using an MIT OpenCourseware class to learn in Python. One of the assignments is to create a program that generates the 1000th prime number starting from 0. One of my first solutions is as follows:

oddList = []
for odd in range(3, 10000):
if odd % 2 != 0:
    oddList.append(odd)
else:
    continue


primeCount = 3
loopHolder = True
while loopHolder == True:

for possiblePrime in oddList:
    if primeCount == 1000:
        print possiblePrime
        loopHolder = False
    from math import *
    limit = int(math.sqrt(possiblePrime)

    for primeTest in range(2, limit):
            testCount = 0
            if possiblePrime % primeTest == 0:
                testCount = testCount + 1
                primeCount = primeCount                
            else:
                continue
            if testCount > 0:
                primeCount = primeCount
                break
            else:
                primeCount = primeCount + 1
                break

However, when I run it, I get a syntax error at "for primeTest in range(2, limit):" and python is highlighting the colon specifically. I realize the error is probably a result of a syntax error somewhere else, but I can't find it. Could someone point out where my error is?

PS: Help with the semantics of the code is not needed, though appreciated.

4

2 回答 2

1

空白对 python 来说非常非常重要。当您编写代码然后将其复制到 stackoverflow 时,您需要更加注意这一点。现在,如果我要复制和粘贴它,您的代码将无法像您编写的那样工作。

对于您的问题,请查看上面的两行,看看您是否缺少右括号。

于 2012-06-05T18:47:37.540 回答
1

您有“while loopHolder == True:”,后面没有缩进块。您可能应该将其写为“while loopHolder:”,因为不需要 == True 部分。我也会避免在循环中进行导入。导入语句通常位于文件的顶部,除非您需要将它放在其他位置。在“limit = int(math.sqrt(possiblePrime)”之后也没有右括号。

于 2012-06-05T18:45:21.333 回答