【发布时间】:2012-06-09 19:18:03
【问题描述】:
我最近决定学习基本编程,并且正在使用 MIT OpenCourseware 课程来学习 Python。其中一项任务是创建一个从 0 开始生成第 1000 个素数的程序。我的第一个解决方案如下:
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
但是,当我运行它时,我在 "for primeTest in range(2, limit):" 并且 python 专门突出显示冒号。我意识到该错误可能是其他地方的语法错误导致的,但我找不到它。有人能指出我的错误在哪里吗?
PS:不需要代码语义方面的帮助,但非常感谢。
【问题讨论】:
标签: python-2.7 syntax-error primes