[Solved] IndentationError: Unindent Does Not Match Any Outer Indentation Level

IndentationError: Unindent Does Not Match Any Outer Indentation Level

IndentationError: unindent does not match any outer indentation level

You must have come across this stupid bug at some point of time while coding. Isn’t it frustrating to get an error even if your code is logically correct!
But after you read this article, you will probably never come across this error again. Even if you do, you will be able to rectify it in a flash! So without further delay, let us dive into our discussion on IndentationError in Python.

Indentation in Python

Indentation refers to the spaces at the beginning of a line of code.

In other programming languages like Java, indentation serves the purpose of readability. Even if you do not follow the proper indentation in such languages, it won’t hamper your code’s execution since they use braces {} to represent a block of code.

In Python indentation is an integral feature that represents a block of code and determines the execution of your code. When you skip proper indentation, Python will throw an IndentationError.

Each line of code within a block should have an equal number of whitespaces before them. If a for loop block contains two lines of code, then each line should have four whitespaces (ideally) before them. If one line of code has three whitespaces while the other has four, you will again get an IndentationError.

â—ˆ Example 1

Here’s the code:

Output:

Solution:

To fix the IndentationError: expected an indented block use the same number of whitespaces for each line of code in a given block.

Output:

✍️ Note:

  • Generally, you should use four whitespaces for indentation and are preferred over tabs.
  • You can ignore indentation in line continuation, but it’s always recommended to indent your code for better readability.
    • For example: if True :print("Welcome To Java2Blog!")

Let’s discuss another example to clarify things further.

â—ˆ Example 2

Consider the following program given below:

Output:

Explanation:

The above error occurred because line 4 , which represents the else block, has an improper indentation. It happened because we mixed TAB and spaces in our code to indent the if-else blocks.

Solution:

Either use TAB or 4 whitespaces (recommended) for both statements (if-else)

Avoid or Fix IndentationError In Code Editors

The primary reason behind IndentationError in Python is the improper use of whitespaces and tabs in your program. In most code editors you can fix the number of whitespace characters/ tabs by setting the indentation levels.

Let us have a look at the settings in various code editors that allow us to auto-indent our code or fix the pre-existing errors :

➥ Sublime Text

  1. Click in View.
  2. Select Indentation.
  3. Select Indentation to tabs.
  4. Uncheck the Indent Using Spaces option in the sub-menu above.

➥ Notepad++

Follow the settings given below to view the tabs or whitespaces in Notepad++.

  1. Click on View
  2. Select Show Symbol
  3. Make sure that Show Whitespace and TAB and Show Indent Guide options are checked.

➥ Using an IDE

The advantage of using an IDE like PyCharm is that you do not have to worry about indentations in your code manually. It is taken care of by the IDE itself, as shown in the presentation given below.

Practice Exercise

Let’s test your knowledge with an exercise. Find the error in the following code and rectify it.

Expected Output:

*
**
***
****
*****

Hint: last line in the code looks fishy! 🕵️

Solution:

Conclusion

I hope you enjoyed reading this article and learned how to fix Indentation Errors in Python. Please subscribe and stay tuned for more exciting articles in the future!

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *