IndentationError: expected an indented block
Why Does This Occur?
As the error implies, this occurs after statements that require indenting, such as after
for loops and
try except exception handling.
Unlike many programming languages that use braces, Python requires indents to determine which code block belongs to a statement. More simply, after detecting the
: character in your script, Python will look for an indent.
This lesson will quickly examine a few reasons when this error can occur and how to fix it.
Cause 1: Unindented Statement
Imagine you are looking at sales figures for Company A, which sells software packages. You want to write a script for determining which employees are meeting a certain sales threshold.
enumerate, we can iterate through employees and use the index as an ID for each employee. We can then print off a message showing if that employee hit the sales target or not.
The script below shows how we can execute this process:
Although we've made the
if else loop correctly, the
for statement is causing an indentation error. This error is happening because we've provided a list for Python to iterate through in our
for loop, but it doesn't know which logic it needs to apply while looping.
The straightforward fix is to add an indent at the line indicated in the error:
Now that Python has the correct structure, it will check the sales figure for each employee individually and consider if the number is greater than 50 or not. It will then print the corresponding message and move on to the next employee.
Cause 2: Empty Suite
When working on larger scripts, you'll often anticipate many
if elif branches ahead of time by creating a branch and commenting on some logic you plan on filling in later.
Here's an example using our sales analysis script that we used previously:
In this case, Python throws the error because it's looking for a code block after the
if statement, i.e., what your program should do if the statement is true. The code seems to be structured correctly, but the program will fail to run until the actual code is placed after the
Having a statement like this without anything following it is known as an empty suite. A quick fix for this is to use the
In this situation, the
pass keyword allows Python to skip when the
if is true. This command bypasses the indentation error, allowing us to work on other areas until we are ready to come back and write the functionality that displays a message.
Cause 3: Unindented Docstring
To keep code well-documented, we can use docstrings at the start of a function, class, or method to quickly say what the code does. This description is to make life easier for yourself and others when reviewing the code later.
To write a docstring, you use two sets of triple apostrophes (''') or quotes ("""), which makes multi-line comments in Python possible.
The example below shows how we can use a docstring to describe a function to contain the if-else loop we've been using in our sales analysis script.
This script crashed because Python is looking for indentation at the start of the function. To fix this, we can add an indent to the docstring. Shown below is this solution in action:
Note that in this example, using a regular comment (#) to mark the docstring would prevent the indentation error without the need to add an indent. Avoid doing this, though, as it's best practice to keep docstrings within two sets of triple apostrophes/quotes.
This error occurs when Python is looking for an indented block of code after certain types of statements. The indented block tells Python that the code within the block is relevant to the statement. This s}tructure is fundamental to the Python programming language, so it's no surprise incorrectly indenting things can make scripts malfunction! Luckily, this is an easy fix, and in most cases, all you need to do is quickly add an indent in the correct place, and you'll be good to go.