Fix: Indentation Error Python
Python is an emerging programming language which was first released in 1991. The language is known for its large comprehensive library and supports several programming paradigms such as functional, imperative, procedural, and object-oriented.
The ‘Indentation Error: Expected an indented block’ occurs for all sorts of users; whether they are newbies or experienced. Since Python arranges all of its code by correct whitespaces, if you have a bad indentation, the code will not compile and you will be returned an error message.
According to the conventions followed in PEP8, there should be four whitespaces where required. It is ideal for every programmer to use proper indentations so code readability improves.
What causes Indentation Error in Python?
Like mentioned before, this error primarily occurs because there are space or tab errors in your code. Since Python uses procedural language, you may experience this error if you have not placed the tabs/spaces correctly. The program may run properly but if the interpreter finds this error, the error message will come forth in the middle. Some of the causes of the error are:
- You are using both spaces and tabs in your code. If both are used interchangeably, the interpreter will not be able to determine which item to use.
- You have placed some indent wrong. If the indentation practice is not followed, you will have this error inevitably.
- You forgot to indent the compound statements such as ‘if’, ‘for’, ‘while’ etc.
- You forgot to indent user-defined functions or classes.
Solution 1: Checking for wrong white spaces/tabs
There is no instant fix for this issue. Since the code is yours, you have to go through each line and see where you made the mistake. There are several blocks in code with respect to the structure. If there is an ‘If’ statement, there needs to be an indentation to the code which follows it.
Check out the diagram above. See that the indentation for a specific block remains the same throughout the code even if a new block is introduced in the middle. Make sure that your indentation is consistent. If you are using spaces, always use spaces and if you are using tabs, always use tabs. The mixing of two will cause issues.
Correct indentation is shown in the example above. See the ‘for’ loop for starters. Everything inside the ‘for’ loop must be indented. Inside ‘for’ loop, we have an ‘if’ statement. Inside the ‘if’ statement, everything must be further indented.
You can easily check where the indentation error occurred by checking the error log and seeing the line from where the error originated.
Solution 2: Enabling tab/space symbols in the editor
If you are having a hard time indenting your code by ‘guess’ as all programmers do, you can enable the symbols of tab/space in your IDE or code editor. This option will enable small ‘dots’ in your code where each dot represents a space or tab. You can use this to indent code more properly and make sure that there is no extra indent or some is missing.
In this example, we will take Notepad++ and see how you can enable the symbols. If you are using another software for code editing, you can enable the setting specific to it.
- Press View > Show Symbol > Show Whitespace and TAB
- Now the option is enabled. You can also enable the Indent Guide so things get easier for you.
Check the example above. See the indentation implemented after each class. Each space is represented by a single dot. After making changes to the wrong indentation on your code, run it again and see if this solves the issue.