IndentationError: unindent does not match any outer indentation level

The python error IndentationError: unindent does not match any outer indentation level indicates that there is excess indent in the line that the python interpreter does not expect to have. In python, the error IndentationError: unindent does not match any outer indentation level occurs when you add excess indents at the beginning of the line. The compound statement and functions requires the indent to be distinguished from other lines. If the indent does not match with any of the outer indentation level, the error will be thrown.

The indent is defined as the distance or number of empty spaces between the start of the line and the left margin of the line. Indents are not considered in the most recent programming languages such as java, c++, dot net, etc. Python uses the indent to distinguish compound statements and user defined functions from other lines.

The error message “IndentationError: unindent does not match any outer indentation level” indicates that there is excess indent in the line that the python interpreter does not expect to have. The indentation error will be thrown as shown below.

  File "/Users/python/Desktop/test.py", line 3
    print "end of program";
                          ^
IndentationError: unindent does not match any outer indentation level
[Finished in 0.0s with exit code 1]


Root Cause

In python, the indent should be added in Compound statement and user defined functions to distinguish it from other lines. The indent is defined as the distance or number of empty spaces between the start of the line and the left margin of the line.

The error message IndentationError: unindent does not match any outer indentation level is thrown because of the excess indent that the python interpreter doesn’t expect to have in the line



Solution 1

In most cases, this error would be triggered by a mixed use of spaces and tabs. Check the space for the program indentation and the tabs. Follow any kind of indentation. The most recent python IDEs support converting the tab to space and space to tabs. Stick to whatever format you want to use. This is going to solve the error.

Check the option in your python IDE to convert the tab to space and convert the tab to space or the tab to space to correct the error.



Solution 2

In the sublime Text Editor, open the python program. Select the full program by clicking on Cntr + A. The entire python code and the white spaces will be selected together. The tab key is displayed as continuous lines, and the spaces are displayed as dots in the program. Stick to any format you wish to use, either on the tab or in space. Change the rest to make uniform format. This will solve the error.

Program

if 10<20:
	print "a is greater";      ----> Indent with tab
        print "end of program";    ----> Indent with spaces

Solution

if 10<20:
	print "a is greater";    ----> Indent with tab
	print "end of program";  ----> Indent with tab


Solution 3

In the python program, check the indentation of compound statements and user defined functions. Follow the indentation in the control and loops. Make sure all the lines follow with the same indentation in a block. This will resolve this issue. In the example below, the if block contains two lines. The first line is indented excessively than expected.

Program

if 10<20:
		print "a is greater";
	print "end of program";

Output

 File "/Users/python/Desktop/test.py", line 3
    print "end of program";
                          ^
IndentationError: unindent does not match any outer indentation level
[Finished in 0.0s with exit code 1]

Solution

if 10<20:
		print "a is greater";
		print "end of program";

Output

a is greater
end of program
[Finished in 0.1s]


Solution 4

In the python program, check the indentation of compound statements and user defined functions. Follow the indentation in the control and loops. Removing of the excess indentation will resolve this issue. In the example below, the if block contains two lines. the first line has excess indentation than the expected.

Program

if 10<20:
		print "a is greater";
	print "end of program";

Output

 File "/Users/python/Desktop/test.py", line 3
    print "end of program";
                          ^
IndentationError: unindent does not match any outer indentation level
[Finished in 0.0s with exit code 1]

Solution

if 10<20:
		print "a is greater";
print "end of program";

Output

a is greater
end of program
[Finished in 0.1s]


Solution 5

In the python program, check the indentation of compound statements and user defined functions. Following the indentation is a tedious job in the source code. Python provides a solution for the indentation error line to identify. To find out the problem run the python command below. The Python command shows the actual issue.

Command

python -m tabnanny test.py 

Example

$ python -m tabnanny test.py 
'test.py': Indentation Error: unindent does not match any outer indentation level (<tokenize>, line 3)
$ 


Solution 6

There is an another way to identify the indentation error. Open the command prompt in Windows OS or terminal command line window on Linux or Mac, and start the python. The help command shows the error of the python program.

Command

$python
>>>help("test.py")

Example

$ python
Python 2.7.16 (default, Dec  3 2019, 07:02:07) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help("test.py")
problem in test - <type 'exceptions.IndentationError'>: unindent does not match any outer indentation level (test.py, line 3)

>>> 
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> ^D



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *