The Python Oracle

IndentationError: unindent does not match any outer indentation level

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Droplet of life

--

Chapters
00:00 Indentationerror: Unindent Does Not Match Any Outer Indentation Level
00:27 Accepted Answer Score 856
00:53 Answer 2 Score 330
01:25 Answer 3 Score 152
01:44 Answer 4 Score 50
02:07 Answer 5 Score 29
02:21 Thank you

--

Full question
https://stackoverflow.com/questions/4923...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #indentation

#avk47



ACCEPTED ANSWER

Score 862


One possible cause for this error is that there might be spaces mixed with tabs for indentation. Try doing a search & replace to replace all tabs with a few spaces.

Try this:

import sys

def Factorial(n): # return factorial
    result = 1
    for i in range (1,n):
        result = result * i
    print "factorial is ",result
    return result

print Factorial(10)



ANSWER 2

Score 154


To easily check for problems with tabs/spaces you can actually do this:

python -m tabnanny yourfile.py

or you can just set up your editor correctly of course :-)




ANSWER 3

Score 51


Are you sure you are not mixing tabs and spaces in your indentation white space? (That will cause that error.)

Note, it is recommended that you don't use tabs in Python code. See the style guide. You should configure Notepad++ to insert spaces for tabs.




ANSWER 4

Score 29


Whenever I've encountered this error, it's because I've somehow mixed up tabs and spaces in my editor.