Python indentation is a stickler. You must correctly indent your code.
If you use the wrong arrangement of spaces and tabs in a Python program, you encounter the “IndentationError: expected an indented block” error.
In this guide, we talk about what this error means and why it is raised. We’ll walk through an example of this error to help you figure out how to solve it in your code.
IndentationError: expected an indented block
Programming languages like C and JavaScript do not require particular indentation. This is because they use curly braces to denote the structure of code blocks. Python does not use curly braces or a similar indicator.
The language depends on indentation to determine the structure of a program. Without indentation, it is impossible for the Python interpreter to know how to read a block of code.
Consider the following Python code:
def find_average(grades): average = sum(grades) / len(grades) print(average) return average
How does Python know what code is part of the find_average()
defined function and what code is part of the main program? This shows why indentation is so important.
Any time you forget to indent code in a program, Python makes you aware of this by raising an indentation error.
An Example Scenario
Let’s write a program that extracts all bagels from a list of lunch foods on a cafe lunch menu. These bagels will be added to their own list.
Let’s start by defining a list of all the foods available at lunch time:
lunch_menu = ["Cream cheese bagel", "Cheese sandwich", "Hummus, avocado, and cucumber sandwich", "Smoked salmon bagel"]
Our lunch menu contains two sandwiches and two bagels. Next, we write a function that creates a new list of bagels based on the contents of our “lunch_menu” list:
def get_bagels(menu): bagels = [] for m in menu: if "bagel" in m: bagels.append(m) return bagels
Our get_bagels()
function accepts one argument: the menu items through which the function should search. Our function iterates through each item on the menu and checks if the item contains the word “bagel”. If it does, that lunch food is added to the “bagels” list.
Finally, we have to call our function in our main program:
bagels = get_bagels(lunch_menu) print(bagels)
This code will call our get_bagels()
function and then print the list of bagels it creates to the console. Let’s run our code and see what happens:
File "main.py", line 4 bagels = [] ^ IndentationError: expected an indented block
Our code fails to execute.
The Solution
An IndentationError tells us we have incorrectly indented our code. The error message shows us that it expects an indent on line four. Take a look at our code:
def get_bagels(menu): bagels = []
The “bagels” variable declaration is supposed to be part of our function but it is not indented. This causes an error because Python functions expect to have at least one line of code indented below where they are declared.
To solve this error, we need to indent our variable declaration:
def get_bagels(menu): bagels = []
Let’s run our code:
['Cream cheese bagel', 'Smoked salmon bagel']
Our code finds all of the bagels in our “lunch_menu” list and adds them to the “bagels” list. Our code then prints out the list of bagels to the console. There are two bagels on the lunch menu.
Conclusion
The “IndentationError: expected an indented block” error is raised when you forget to add an indent in your code.
To solve this error, make sure your code contains the proper number of indents. Now you have the knowledge you need to fix this error like an expert coder!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.