Not all error messages result from problems with your code itself but how that code is formatted. Formatting is important in the Python programming language. Indentations are an important part of Python formatting and making a mistake in this area could cause the programs to do weird things. It can however also result in an error message.
What is This Error?
The indentationerror: unindent does not match any outer indentation level is a particularly annoying error because it does not show up visibly. It is an easy error to make and fix, but it is difficult to find. Because this is a python indentation error it results from both tabs and whitespaces being used for a python indentation. Because on the screen a tab key and whitespace look the same in most editors despite actually being different characters. Unfortunately, this is at the heart of why this improper indentation error occurs in your code block, and why trying to find the cause of the error and create correct indentation or an indented block is difficult.
How Does This Occur?
A python proper indentation can be done using tabs or spaces. You are free to use either one, but you cannot use both together. When you mix them within indented python code, the internal indentation level will not match any outer indentation level, resulting in inconsistent indentation, a mixed tab, or similar error messages. This problem can usually occur when copying and pasting a user-defined function into your python program because the text may not be consistent. However, you will get this similar error messages anytime you mix tabs and spaces for inconsistent indentation. What makes it difficult to find because of the fact that superficially they look alike.
Solutions
Fixing the indentationerror: unindent does not match any outer indentation level error message is quite easy. Finding the error is more difficult. One trick you can use to distinguish between tabs from spaces and find wrong indentation is to load your code into a text editor called sublime text. Sublime text will distinguish tabs from spaces. When you hover over a line you get a dash for a tab and a dot for a space. Actually fixing the error means making sure that you use the same convention when you indent your code. For example, whenever you indent your code make sure you hit the tab key, instead of the space key.
The indentationerror: unindent does not match any outer indentation level error message is an easy mistake to make but it also easy to fix. The hard part when dealing with this indentation problem is finding the error among all those indented code blocks. However, once you find it it is easy to fix. It can be fixed even if you do not find the actual point where the error occurs. All you need to do is make sure that all your indented code blocks use the same convention.
Looking for more helpful Python content? Check out these other great articles:
- typeerror: a bytes-like object is required, not ‘str’
- valueerror: setting an array element with a sequence
- the truth value of an array with more than one element is ambiguous. use a.any() or a.all()
- only integer scalar arrays can be converted to a scalar index