Codes in different programming languages are structured into blocks using specific characters as delimiters. These delimiters include:
- Indentation in Python,
- Begin/end in Pascal,
- Do/end in Ruby, and
- Parentheses in Lisp.
The use of indentation as a delimiter for code blocks in Python is not only a language-specific syntax, but it also enhances the readability of the code.
Indentation in Python can be done using either tabs or spaces. PEP 8 styling guide says:
“Use 4 spaces per indentation level“.
Causes of IndentationError
If Python code is not indented properly, Python raises IndentationError or TabError under the following three circumstances.
Reason 1: Missing indentation error
Indentations are required in the following cases (not an exhaustive list):
- At function or class definition,
- In loops – for and while loops,
- In Conditional statements – if-else statements,
- In try-else statements,
- When using with statement.
The following Figure shows indentations on for-loop and if-else statement.
When indentation is not provided at an expected position. In such a case, Python raises the
“IndentationError: expected an indented block…” error.
num = 7
IndentationError: expected an indented block after 'if' statement on line 2
Reason 2: Unexpected indentation
If you provide indentation outside the requirements listed above, it is most likely you are providing an unnecessary indentation, for which Python will raise the
“IndentationError: unexpected indent” error.
name = "Smith"
IndentationError: unexpected indent
Reason 3: Mixing tabs with spaces
Mixing tabs and spaces in one block is not allowed in Python 3.x. If you do that, you will get the following error:
“TabError: inconsistent use of tabs and spaces in indentation“.
This error can be challenging to identify by visually inspecting your code because, in most cases, a single tab may have the same width as four spaces. We will see how to nail this error in the solutions.
Reason 4: Indentation not matching any block
This occurs when your indentation does not match any of the previous blocks. For that, the following error is raised:
“IndentationError: unindent does not match any outer indentation level“.
In the Figure above, the return statement doesn’t match any previous block. That is because there’s an extra space before the return keyword.
Solutions to the IndentationError
There are two solutions you can use to solve IndentationError.
Solution 1: Correct the indentation problem using the error message
Whenever Python raises the IndentationError, it provides information on the line causing the error (see Figure above). In that case, you can easily find the line and add or remove indentation based on the nature of the error.
Solution 2: Configure your IDE or code editor to fix indentation problems
Most IDEs and code editors provide settings for setting the indentation delimiter – tab or spaces. For example, in VS Code and Sublime Text, those options should be in the bottom right corner. That should help solve most indentation problems as you code.
In some cases, however, you already have problems in your code – especially mixed tabs and spaces. For this scenario, most IDEs also provide options to convert tabs to spaces and vice versa. On VS Code and Sublime Text, you should also get those options in the bottom right corner of your editor.
Here is how it is in VS code.
Indentation is part of Syntax in Python. In this article, we covered four reasons that cause IndentationError or TabError in Python and how to solve them.
We discussed two solutions. The first solution you can use to solve these errors is to read the error message raised by Python and then fix the IndentationError accordingly. Secondly, deploy configuration options provided by your code editor or IDE to solve Indentation problems.