Blogs

Unindent Does Not Match Any Outer Indentation Level-Python

You would have encountered a lot of errors during the journey of becoming a programmer. TypeError, ValueError, ZeroError and so on. Life without error just sucks, nothing to accomplish and overcome. Here we are going to discuss indentation error. Why it happens? and how to get rid of it? The problems will be explained and solved along with code snippets. Let's dig in!

Indentation and its Functionality?

Indentation is a space left before starting a new line, which indicates that a new paragraph has started. In terms of a Programming World, indentation is used to indicate Scope in a code. Python is one of the top-ranked languages in the world that uses the indentation method. Indentation is used to point out a specific block of code. Programming languages like C++, JAVA don’t use indentation for this purpose instead they use curly braces ( “{ }” ). Indentation’s other main use is to beautify your code and doesn’t make it look messy.

Scope: The scope of code tells the interpreter “ the sequence of code execution” By sequence I mean which code block to execute at first and which code block to execute at last.

Indentation Error:

Specifically, we are going to use Python language. If you are an avid Programmer you must have encountered this type of an error displaying:

Unindent Does Not Match Any Outer Indentation Level-Python

Now there are several reasons why this error appears.

You are using the Tabs and spaces in the same code to make indentations, although your code looks perfect and is expected to work fine still it throws an error.

Another reason is that indentation practice is not followed by the code block, one line is more indented than the other one. In python Conditional statements (if, else, elif), functions ( are created using “def” keyword) and loops (for, while) uses a colon, and after using a colon the upcoming line needs to indented.

Note: An indentation can be of one tab, 4 spaces, even a single space but once the indentation length is set, all lines must be indented accordingly. then your code will work perfectly.

CODE: (indentation of only single space)

Unindent Does Not Match Any Outer Indentation Level-Python

OUTPUT:

Unindent Does Not Match Any Outer Indentation Level-Python

In the example below, 👇👇 we created a variable “weather_hot” and then used an “if conditional”. We used two print statements but the first with more indentation, which according to the Python interpreter is not legal and thus it throws an indentation error.

CODE:

Unindent Does Not Match Any Outer Indentation Level-Python Unindent Does Not Match Any Outer Indentation Level-Python

OUTPUT (Error):

Unindent Does Not Match Any Outer Indentation Level-Python

Python follows the PEP8 whitespace Rule, which depicts that each line that is indented must have 4 whitespaces. Now you can also set the indentation size in your code editor we will talk about that later.

When we use tabs and spaces at the same time the interpreter is not able to figure out which line of code to execute or not. Below is another example of when an indentation error occurs.

In this example, we have created a function that will return the factorial of a number

Let me break down this code. On the first line, we used the “def” keyword to create a function, according to PEP8 rule the next line must be indented, this is our first block of code. After this, we created a “for” loop that ends with a colon. Now the next line is our second block of code. But the problem arises when we incorrectly indent line 5 out of the block. Due to wrong indentation Line5 is not present in any of the code blocks . while executing the interpreter gets confused and thus throws an Indentation error.

CODE (ERROR):

Unindent Does Not Match Any Outer Indentation Level-Python

OUTPUT (Error):

Unindent Does Not Match Any Outer Indentation Level-Python

CODE (with correct indentation):

Unindent Does Not Match Any Outer Indentation Level-Python

OUTPUT:

Unindent Does Not Match Any Outer Indentation Level-Python

How to solve Indentation Error:

Now , you know why this error is occurring. Let’s fix it!

You have to explicitly check each line of code as if there is any wrong indentation. Make sure that either all tabs are used or spaces. But there is a little problem, How we can judge if we have used space or a Tab. There is one way you can achieve this. Open up your code editor, I am using notepad++ for this.

On the top menu, you will find “view”. By clicking on view A dropdown menu will appear having an option “show symbol” where you can choose either to display tabs and spaces in your code. Once you checked the option.

You will see dots or arrows in your code. The arrow displays tabs and dots indicate spaces. This is how you can check your code and see the root of the error.

Quick Guide = view > show symbol > Show White Space and Tab

Unindent Does Not Match Any Outer Indentation Level-Python
  • Dots represent Spaces
  • Arrows represent Tabs
Unindent Does Not Match Any Outer Indentation Level-Python

Indentation Setting in Sublime Text 3:

You can easily set indentation setting in sublime by following the steps below:

  • Open up your sublime text editor.
  • Click on the “view” from the top menu.
  • After clicking! A dropdown menu will appear. Click on the indentation.
  • Another dropdown menu will appear where you can set your indentation size. You can also convert indentation from spaces to tabs and vice versa.
Unindent Does Not Match Any Outer Indentation Level-Python

After setting up all this you are good to go and Next time you see this error 👇👇

Unindent Does Not Match Any Outer Indentation Level-Python

Don’t worry just follow the given steps.

Conclusion:

Indentation is a little special kind of error, unlike other errors, most of the time it is caused due to tabs and spaces, not because of a syntax mistake.