Python Indentation Error: Unindent Does Not Match Any Outer Indentation Level Solution

Indenting code can be a tricky endeavor if you try to use both spaces and tabs simultaneously. Unfortunately, in Python this combination of indentation methods results in an error: “indentationerror: unindent does not match any outer indentation level”.

This guide explains what this error message means and when it occurs. We also provide an example of this issue in action so you can learn how to resolve it.

By continuing, you agree to our Terms of Service and Privacy Policy as well as receive offers and opportunities from Career Karma via telephone, text message, and email.

Error: Unindent Does Not Match Any Outer Indentation Level Solution

Python code can be indented using tabs or spaces – the choice is yours!

Python only objects when you use both spaces and tabs for indentation in your code. This is because Python is statically typed, and statically typed programming languages tend to be strict about syntax.

Indentation errors can be particularly tricky to decipher since they involve the invisible: mixing spaces and tabs. Depending on which code editor you use, you may not even be able to tell whether spaces or tabs have been used until after deleting all indents from your code.

IndentationErrors are common when copying code snippets from the internet. Every developer has their own preferences when it comes to indentation, and you may find that these code samples don’t adhere to your preferences. Some snippets will even indent with spaces!

If you copy and paste a code snippet into your program with different type of indentation, you may encounter an IndentationError in your code due to mixing tabs and spaces.

Begin by creating a function to calculate the factors of an integer:

def get_factors(number):
for i in range(1, number + 1):
if number % i == 0:
print(“{} is a factor of {}.”.format(i, number))

This function loops through every number in between 1 and the specified plus 1.

At each iteration, our program checks if there is a remainder after dividing “number” by “i”. To do this, we use the modulo operator. If there isn’t one, “i” becomes a factor of “number”.

If a number is less than the factor of “number”, an error message will be printed to the console.

We to call our function:

This code calculates all the factors of 32. Run our code and see what happens:

File “”, line 4
print(“{} is a factor of {}.”.format(i, number))

IndentationError: Unindent does not match any outer indentation level
Our code returns an IndentationError.

The Solution

Our code’s styling has been incorrect. This is evident by IndentationErrors being raised when there is an issue with how your code is indented.

Take a closer look at how our code is styled. In Sublime Text, we can view the styles of our code by hovering over each line:

ZkT2zfCE1h22 Ldf RwNqSrZL9fUBLG VdcBcqh559l QY6dDanxnhIiihRf52m10qUKnPgFyT2uG6YxVF5MoRzAVitrOwbnF 04EUYhdD3dMZZXkO3aOERWcZYtYsJpOFu1rqun
Each line represents a tab, while each dot stands for space. As you can see, our code snippet has both spaces and tabs mixed up together. Python requires consistency when indenting strings, so when we run it the interpreter returns an error.

If your text editor does not support this behavior, backspacing your indentation will tell you whether your code uses spaces or tabs. If pressing the backspace key removes a tab when indented, that part of your code is using tabs.

Let’s revise our code:

def get_factors(number):
for i in range(1, number + 1):
if number % i == 0:
print(“{} is a factor of {}.”.format(i, number))

We have now replaced all spaces with tabs. Run our program again:

1 is a factor of 32.
2 is another factor of 32,
4 another factor of 32 and 8,
16 yet again another factor for 32 to make our equation complete.
So now our code successfully returns all factors of 32 – proof positive that our indentation was incorrect all along! This proves our code to be logically sound throughout, though its output was somewhat messy at times due to unexpected errors.


An “indentationerror: unindent does not match any outer indentation level” error will appear if you use both spaces and tabs to indent your code.

To fix this error, double-check that your code uses either spaces or tabs throughout. Now you’re ready to tackle this common Python error like a pro software developer!

Leave a Comment