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.

Indentation Error Python while coding
Indentation Error in Python

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.

Block structure visualizing indentation in Python
Block structure visualizing indentation

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.

Indentation example in Python
Indentation example

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.

  1. Press View > Show Symbol > Show Whitespace and TAB
    Enabling Whitespace and Tab in Python
    Enabling Whitespace and Tab – Notepad++
  1. Now the option is enabled. You can also enable the Indent Guide so things get easier for you.
Sample code with correct indentation in Python
Sample code with correct indentation

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.

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.