. Advertisement .
..3..
. Advertisement .
..4..
Indenting your code is something that not many people pay attention to. After all, it’s only for appearance for most programming languages. However, that is not the case with Python, as it can be very picky with what you use to indent.
One of the biggest problems that this issue can lead to is the infamous indentationerror: unindent does not match any outer indentation level in Python. We prepared this article to dissect this error to make it more manageable for beginners.
What Is Indentationerror: Unindent Does Not Match Any Outer Indentation Level In Python?
It is common knowledge that Python is reliant on indentation to separate code blocks. That is why indentation is especially important in this programming language. It is also the reason it offers two ways to indent, spaces and tabs.
You can freely use one of the two as your indentation, but Python doesn’t like it when you utilize both of them simultaneously. The reason lies in this language’s statically typed nature. For those who don’t know, all statically typed languages are notorious sticklers, especially when the syntax is in the conversation.
When you mix the two methods together, you get the message indentationerror: unindent does not match any outer indentation level in Python. This error appears the most when the user copies some code snippets straight away from the Internet.
Each developer has their own preferences when it comes to formatting in general and indentation in particular. If you happen to have the opposite habit to indentation and you don’t check the source code, you will encounter this error.
This is the reason it’s as frustrating as Keras AttributeError.
Example Of Indentationerror: Unindent Does Not Match Any Outer Indentation Level In Python
Here is a small example to show you how the error appears:
def factors(num):
for z in range(1, num + 1):
if num % z == 0:
print("{} is a {}’s factor.".format(z, num))
At first glance, there is no apparent problem with this code. It is a simple for loop, iterating each number between 1 and a number the user specifies plus 1. With each iteration, the program checks to see any remainder from dividing num by z. It marks z as a num’s factor when there is no remainder.
Whenever there is some remainder, the program prints a message.
However, when you call the function to check, like
get_factors(20)
There will be an error message instead of the program printing. When you check again, there is absolutely no issue in the code’s logic or mistake in the commands.
Solution To Indentationerror: Unindent Does Not Match Any Outer Indentation Level In Python
You can only see what’s wrong with the example below when you paste it into an editor, such as Sublime Text. Hovering on the lines, you will see that there are lines and dots. While the dots signify spaces, lines represent tabs. That’s the issue, we mixed spaces and tabs together.
Another quick way to check for consistency in indentation is to backspacing the indentation on each line. The length of each erasure will tell you immediately if there is a mix-up.
When you find the issue, it becomes very easy to fix. Simply pick a style and revise the code to stick with that style.
Now, when we run, the code will print out:
1 is a 20’s factor
2 is a 20’s factor
4 is a 20’s factor
5 is a 20’s factor
10 is a 20’s factor
20 is a 20’s factor
Conclusion
This article has informed you clearly what indentationerror: unindent does not match any outer indentation level in Python is. There is also a clear example of this error and the way to spot and then fix it. As long as you follow the instructions, this issue will no longer bother you.
Leave a comment