The Python Oracle

IndentationError: unindent does not match any outer indentation level

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Over Ancient Waters Looping

--

Chapters
00:00 Question
00:36 Accepted answer (Score 844)
01:00 Answer 2 (Score 327)
01:36 Answer 3 (Score 152)
01:56 Answer 4 (Score 49)
02:22 Thank you

--

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

Answer 1 links:
[Indentation]: https://www.python.org/dev/peps/pep-0008...
[Tabs or Spaces?]: https://www.python.org/dev/peps/pep-0008...

Answer 3 links:
[style guide]: http://www.python.org/dev/peps/pep-0008/

--

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.