1

using Python I'm currently attempting Project Euler Problem #7 which asks to find the 10001st prime number, however when running my code I encounter the error

"File "<stdin>", line 3
    TestedValue = 2
    ^
IndentationError: unexpected indent
Unknown error."

I'm assuming that isn't the only line which is facing that error but that's the first one that pops up, any idea what I'm doing wrong? Thanks SO!

Code:

def PrimeNum(NumIn):
    PrimeNumCounter = 0
    TestedValue = 2
    if TestedValue == 2:
        PrimeNumCounter += 1
        TestedValue += 1
    else: 
        while PrimeNumCounter != NumIn-1
            for i in range(2, TestedValue):
                Prelim = 0
                if TestedValue % i != 0:
                    Prelim += 1 
                elif TestedValue % i == 0:
                    Prelim = 0 
                if Prelim > 0:
                    PrimeNumCounter += 1
        if PrimeNumCounter == NumIn-1:
            for i in range(2, TestedValue):
                Prelim = 0
                if TestedValue % i != 0:
                        Prelim += 1 
                elif TestedValue % i == 0:
                    Prelim = 0 
            if Prelim > 0:
                print TestedValue
poke
  • 369,085
  • 72
  • 557
  • 602
  • 1
    Once you set the indentation you have to abide by it. You've got your blocks indented by 4 spaces or so everywhere except after that first if statement, where it's indented 8. – Two-Bit Alchemist Aug 18 '15 at 19:54
  • Whenever it gives you an unexpected indent, check the line above it. Also, make sure that your indents aren't tabbed. Do 4 spaces per level of indent. – mauve Aug 18 '15 at 19:55
  • 2
    You may have mixed tabs and spaces. – Cody Piersall Aug 18 '15 at 19:55
  • You also have a syntax error on `while PrimeNumCounter != NumIn - 1` line. You need a colon there. – Cody Piersall Aug 18 '15 at 19:57
  • @Two-Bit Alchemist & others I added those spaces only when submitting the question because the formatting instruction said to add four spaces when submitting code. Sorry, new to this. –  Aug 18 '15 at 20:00
  • 1
    No problem! The problem is that the code here _doesn't actually have the indentation error_. If you copy it here and paste it into a file and run it, it will complain about the syntax error I mentioned above. – Cody Piersall Aug 18 '15 at 20:02
  • @CodyPiersall alright so should i just replace all tabs with spaces and edit my question? –  Aug 18 '15 at 20:02
  • I think you can just delete the question, really. I asked some people in the Python chatroom and they agreed (or were at least quiet). – Cody Piersall Aug 18 '15 at 20:17
  • `while PrimeNumCounter != NumIn-1` should be `while PrimeNumCounter != NumIn-1:` – Vineet Kumar Doshi Aug 19 '15 at 04:00

1 Answers1

3

Your text editor is mixing spaces and indents. Python can only handle one or the other.

To fix,

Use the reindent.py script that you find in the Tools/scripts/ directory of your Python installation:

Change Python (.py) files to use 4-space indents and no hard tab characters. Also trim excess spaces and tabs from ends of lines, and remove empty lines at the end of files. Also ensure the last line ends with a newline. Have a look at that script for detailed usage instructions.

from How to fix python indentation

Community
  • 1
  • 1