Python uses indentation to define code blocks. You can use either spaces or tabs to indent the code. The indent can be any size, but it must be exactly the same level. The mix of spaces and tabs or a mismatch in indent size will cause the error IndentationError: unindent does not match any outer indentation level
Your python code looks to be correct. The error, however, is most likely caused by tabs and spaces. The editors you use show indent to the same position. However, if you mix spaces and tabs, the compiler has no idea how many spaces a tab is supposed to represent. The Python compiler threw the error IndentationError: unindent does not match any outer indentation level.
You can’t have one line indented with tabs and the next with spaces. Tabs and spaces cannot be used interchangeably. You must make a decision; you indent either with tabs or with spaces, but not both. The indent can be any size, but the indent must match exactly to be at the same level.
You can identify and correct this IndentationError: unindent does not match any outer indentation level error using the techniques listed below.
1. Check mix of spaces & tabs
In most cases, this error would be caused by a mix of spaces and tabs. Check the space for the program indentation and the tabs. Follow any type of indentation, such as spaces or tabs, but not both. The most current Python IDEs support the conversion of tabs to spaces and spaces to tabs. Stick to whichever format you choose. This will resolve the issue.
Look for the option to convert the tab to a space in your Python IDE. Change the tab to space or the tab to space to resolve the problem.
2. Verify tabs and spaces used in lines
Open the python program with the sublime Text Editor. By pressing Cntr + A, you may access the entire program. The complete python code as well as the white spaces will be chosen at the same time. In the program, the tab key is represented by continuous lines, and spaces are represented by dots. Stick to the format you want to use, whether it’s on a tab or in space. Change the rest to a more consistent format. This will solve the problem.
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
3. Mismatch of Indent size in code block
In the Python program, double-check the indentation of code blocks such as compound statements and user-defined functions. In a coding block, make sure all lines have the same indentation. This will resolve the issue. In the following example, the if block comprises two lines. The first line is more indented 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]
4. Mismatch of Indent size outside of code block
In the python program, check the indentation of lines outside of code blocks, such as compound statements and user defined functions. There might be extra indent on the lines outside of the code block. This problem may be solved by removing the extra indentation. The if block in the following example has two lines. The second line has more indentation than is typical.
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]
5. Debug using python -m tabnanny
Check the indentation of compound statements and user defined functions in the Python program. In the source code, following the indentation is a time-consuming task. Python provides a technique for identifying the indentation error line. Run the python script below to troubleshoot the problem. The Python command displays the exact problem.
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)
$
6. Debug using python help
There is another method for identifying the indentation problem. Start Python by opening the command prompt in Windows or the terminal command line window in Linux or Mac. The help command displays the python program’s error.
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