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.
You have “while loopHolder == True:” without an indented block after it. You should probably write this as “while loopHolder:”, as the == True part isn’t required. I would also avoid doing the import within the loop. Import statements are usually at the top of the file, unless you need it to be somewhere else. You also don’t have a closing bracket after “limit = int(math.sqrt(possiblePrime)”.