Table of Contents
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:
1 2 3 4 5 6 7 8 |
for i in range(1,6): if i%2==0: print(i,' is even') else: print(i," is odd") |
Output:
1 2 3 4 5 6 7 8 9 |
File "D:/PycharmProjects/pythonProject1/IndentationError.py", line 4 else: ^ IndentationError: unindent does not match any outer indentation level Process finished with exit code 1 |
Solution:
To fix the IndentationError: expected an indented block
use the same number of whitespaces for each line of code in a given block.
1 2 3 4 5 6 7 8 |
for i in range(1, 6): if i % 2 == 0: print(i, ' is even') else: print(i, " is odd") |
Output:
1 2 3 4 5 6 7 |
1 is odd 2 is even 3 is odd 4 is even 5 is odd |
✍️ 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!")
- For example:
Let’s discuss another example to clarify things further.
â—ˆ Example 2
Consider the following program given below:
1 2 3 4 5 6 7 |
age = 25 if age<18: print('You cannot Vote!') else: print('You can Vote!') |
Output:
1 2 3 4 5 6 |
File "<tokenize>", line 4 else: ^ IndentationError: unindent does not match any outer indentation level |
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
)
1 2 3 4 5 6 7 8 9 |
age = 25 if age<18: print('You cannot Vote!') else: print('You can Vote!') # Output: You can Vote! |
✨ 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
- Click in
View
. - Select
Indentation
. - Select
Indentation to tabs
. - 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++.
- Click on
View
- Select
Show Symbol
- Make sure that
Show Whitespace and TAB
andShow 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.
1 2 3 4 5 6 |
for i in range(6): for j in range(i): print('*',end="") print() |
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!