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.

Pen275
  • 21
  • 5

2 Answers2

1

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)".

xioxox
  • 2,526
  • 1
  • 22
  • 22
1

White space is really, really important for python. You need to pay more attention to that when you are writing the code and then copying it over to stackoverflow. Right now your code won't work as you have written it if I were to copy and paste it.

For your problem, look at two lines above and see if you are missing a closing parenthesis.

Brian Bruggeman
  • 5,008
  • 2
  • 36
  • 55
  • [*indentation* is important in Python, not whitespace](http://www.secnetix.de/~olli/Python/block_indentation.hawk) – jfs Jun 05 '12 at 19:05
  • 1
    Whitespace is so much more than just indentation. It allows people to see or be confused. It allows things to be segregated visually. While python primarily enforces just indentation, whitespace is significant enough to be part of [PEP-8](http://www.python.org/dev/peps/pep-0008/). I think that blog post is more unfortunate than helpful in teaching people new to python or programming about the merits of white space. – Brian Bruggeman Jun 06 '12 at 06:39
  • couldn't agree more, even in the context of [other programming languages, like Prolog](http://stackoverflow.com/questions/318888/solving-who-owns-the-zebra-programmatically/8270393#8270393). – Will Ness May 21 '14 at 09:04