When I was developing a game, I found a Python script that worked perfectly, wrote a few lines of code, and made sure they all aligned, but ran with a syntax error:

IndentationError: unindent does not match any outer indentation level

[Solution]

Here I will provide two environments for error change:

Nodepad++ :

1. The most common reason for this error is that there really is no alignment. But I took a look at the code based on the number of lines in the error message, and there was nothing wrong. It’s all TAB aligned, no misaligned lines. 2. Delete the previous comment because the content of the previous comment affects the syntax of the following statement. The result is the same syntax error. 3. After a while, it occurred to me to display all the characters in the current Python script to see if there were any special characters.

The current text editor Notepad++ seems to have a setting to display all characters. If found, go to: View -> Show Symbols -> Show Spaces and tabs

The number of lines in error is 1580:

But the source line 1580 alignment is dotted with Spaces, and the arrow used to align the previous lines represents the TAB key, which is mismatched, i.e. the alignment of the code, using a mix of TAB and space:



The new Python syntax does not support the use of tabs and Spaces in code alignment. This is why the above error message appears.

The solution is simple: to change the corresponding TAB, all Spaces, unified alignment style, can be. In Notepad++, go to: Settings -> preferences:

Language -> Replace space (TAB key) :

Can be implemented, for the future each TAB input, are automatically converted to 4 Spaces.

Sublime does Text3:

The conversion is controlled by the command directly under the view-indentation

【 summary 】

If IndentationError is encountered in Python, the first thing you should think about is whether the TAB key and space key are mixed.

It is estimated that many people are also caused by such reasons. Just remember to keep it in line.