The IndentationError in Python is a type of error that occurs when you write your code with incorrect indentation. In this short article, we will discuss the reasons for getting IndentationError: Unindent Does Not Match Any Outer Indentation Level error and will go through various scenarios to solve the issue.

Solve IndentationError: Unindent Does Not Match Any Outer Indentation Level in Python
The IndentationError: Unindent Does Not Match Any Outer Indentation Level error occurs when you write the code with incorrect indentation. Now, what is an indentation in Python? Indentation in Python refers to the spaces at the beginning of each line in the code. In other programming languages, Indentation is used to make code readable while in Python it is a must. Without proper indentation, we will get IndentationError.

In this section, we will go through various possible cases of getting this error and will solve them.
Mixing Tabs and Spaces
Usually in Python, if any statement is ending with a colon, then the next line starts with a tab or 4-spaces and any statement under that colon one should be written after 4 spaces. Let us take a simple example and see when we are getting this error. In our example, we will have a function that will print “Hello” and “World”
def my_function():
if True:
print("Hello")
print("World")
my_function()
Output:
File <tokenize>:4
print("World") # This line has an extra space at the beginning
^
IndentationError: unindent does not match any outer indentation level
The error occurs because the second print statement is not matching with any of the indentations previously. If the second print statement is inside the if statement, then it should be aligned with the previous print statement as shown below:
def my_function():
if True:
print("Hello")
print("World")
my_function()
Output:
Hello
World
As you can see, we got the desired output.
Incorrect number of spaces
We get the error when we have specified an incorrect number of spaces after a statement. For example, see the example below:
def Name():
print("This is ")
print("Bashir")
Name()
Output:
File <tokenize>:3
print("Bashir")
^
IndentationError: unindent does not match any outer indentation level
As you can see, the error is because the print statement is not correctly indented. We can solve the problem by using proper indentation as shown below:
def Name():
print("This is ")
print("Bashir")
Name()
Now, this code has the correct indentation and we will not get any errors when we run it.
Understanding IndentationError: Unindent Does Not Match Any Outer Indentation Level Error
The error clearly says that there is an indentation problem with our code which means we are not using tabs or spaces properly before starting the statement. If you will look closely at the error, it will be something like this:
File <tokenize>:3
print("Bashir")
^
IndentationError: unindent does not match any outer indentation level
The ^ operator shows the line where we have an Indentation problem and the first line after “File” gives the line number where we have the problem. Using this information, we can directly jump to the line to solve the problem.
Possible Reasons for Getting Indentation Error
“IndentationError: unindent does not match any outer indentation level” is a common error in Python that occurs when there is a problem with the indentation of your code. The following are some common cases that can result in this error:
- Inconsistent indentation
- Mixing tabs and spaces
- missing indentation
- incorrect use of whitespaces
- incorrect use of comments
Summary
The IndentationError: Unindent Does Not Match Any Outer Indentation Level error occurs when we use incorrect tabs or spaces before starting a statement in Python. In this short article, we discussed how we can solve the problem and highlighted the main causes of the issue.
Related Errors
- [Solved] Attributeerror: module matplotlib has no attribute subplots
- AttributeError: ARIMAResults Object Has No Attribute plot_predict
- Reverse a String in Python Using 7 Methods
- TypeError: Not Enough Arguments For Format String in Python
- TypeError: Not Enough Arguments For Format String in Python