Indenting your code can be a messy business if you try to use both spaces and tabs. In Python, using both methods of indentation results in an error. This error is “indentationerror: unindent does not match any outer indentation level”.
In this guide, we talk about what this error means and when it is raised. We walk through an example of this error in action to help you learn how to fix it.
indentationerror: unindent does not match any outer indentation level
Python code can be indented with tabs or spaces. It’s up to you.
Python only has an objection when you use both spaces and tabs to indent your code. Python requires you use only one method of indentation. This is because the language is statically typed. Statically typed programming languages are sticklers when it comes to syntax.
Indentation errors are a difficult one to understand because it involves the invisible: mixing spaces and tabs. Depending on the code editor you are using, you may not even be able to see whether spaces or tabs have been used until you delete your indents from your code.
IndentationErrors are common when you copy code snippets from the internet. Every developer has their own preference when it comes to indentation and you’ll often find code snippets do not adhere to your own preferences. Some snippets will indent with spaces.
If a code snippet you copy into your program uses a different type of indentation, you may see an IndentationError in your code. This is because you mix tabs and spaces.
An Example Scenario
Write a program that finds the factors of a number.
Start by defining a function to calculate the factors of a number:
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 uses a for loop to iterate through every number in the range of 1 and the number we have specified plus 1.
In each iteration, our program checks if there is a remainder after dividing “number” by “i”. We do this using the modulo operator. If there is not a remainder, “i” is a factor of “number”.
If a number is a factor of “number”, a message is printed to the console.
We to call our function:
get_factors(32)
This code calculates all the factors of 32. Run our code and see what happens:
File "test.py", 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
We have made a mistake with the styling of our code. We know this because IndentationErrors are raised when there is a problem with how your code is indented.
Take a look at how our code is styled. In Sublime Text, we can see the styles of our code by hovering over each line:
Each line represents a tab. Each dot represents a space. You can see that we have mixed up both spaces and tabs in our code snippet. Python likes consistency in indents and so the interpreter returns an error when we run our code.
If you use a text editor that does not support this behavior, check whether your code uses spaces or tabs by backspacing your indentation. If your code removes a tab when you press the backspace key, that part of your code is using tabs.
Spaces are the preferred method of indentation in Python but you can use tabs if you want.
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 replaced all the spaces with tabs. Run our program again:
1 is a factor of 32. 2 is a factor of 32. 4 is a factor of 32. 8 is a factor of 32. 16 is a factor of 32. 32 is a factor of 32.
Our code successfully returns a list of all the factors of 32. This shows us that our code was logically accurate all along. It was our indentation that caused the problem.
Conclusion
The “indentationerror: unindent does not match any outer indentation level” error is raised when you use both spaces and tabs to indent your code.
To solve this error, check to make sure that all your code uses either spaces or tabs in a program. Now you’re ready to resolve this common Python error like a professional software developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
Thankyou so much it help me alot to know what number of indentataion i have mistaken