anon
anon

Reputation:

Python: Indentation Errors

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

Upvotes: 1

Views: 549

Answers (1)

user2349270
user2349270

Reputation: 61

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

Upvotes: 3

Related Questions