Although indentation is a really good method to write clear and clean code but for some languages, it is mandatory including Python. If you will not write the Python code with proper indentation, you will get IndentationError: expected an indented block error. The error clearly says, there is an indentation problem which means we are trying to write the code without any proper indentation. In this article, we will learn how you can solve the IndentationError: expected an indented block error in Python using simple methods.
Solve IndentationError: expected an indented block in Python
You usually face, this error when you just start the Python programing language. Unlike many other programming languages, in Python, it is a must to write the code with proper indentation. In this section, we will go through various reasons for getting this error and will solve the error using proper indentation.
Before going to the solution, let us try to understand what indentation means in Python. In Python, indentation actually refers to the spaces at the beginning of the code line. There are some rules for the indentation and while writing Python code, we have to take care of these rules properly. For example, anything that you write inside a function should have proper indentation ( usually 4 spaces before each line).
Every line starts with some spaces ( indentation). If we will write the statements inside the if statement or the function without an indentation, then we will get IndentationError: expected an indented block error.
Now, let us go through possible reasons for getting this error and solve the error.
Proper way of defining a function in Python
The very common mistake that beginners do is they forget to put the indentation for the statement that is written inside the Python function. A function in Python is a block of code that only executes when is being called.
The following is the incorrect way to define a function:
# defining a function
def main():
var = 10
print(var)
# calling the function
main()
Output:
File "/tmp/ipykernel_272756/1223864513.py", line 4
var = 10
^
IndentationError: expected an indented block after function definition on line 2
The reason for getting this error is that we have not put any spaces before the statements that are inside the function. In Python, you can put the indentation by selecting the statements and then pressing the tap key on the keyboard.
The following is the correct way of defining a function.
# defining a function
def main():
var = 10
print(var)
# calling the function
main()
Notice that this time, the statements that belong to the function are inside the function which makes the code clear and clean as well.
Indentation after if-else statements
If you are using if-else statements in your code, then make sure that every statement, that belongs to the if or the else block should be properly indented. The if-else statements in Python are used for the conditions:
The following is an example of incorrect if-else statements which raise an error:
# defining a variable
var = 10
# if else statements
if var > 5:
print("True")
else:
print("False")
File "/tmp/ipykernel_272756/1573069851.py", line 6
print("True")
^
IndentationError: expected an indented block after 'if' statement on line 5
The reason for getting this error is that there is no proper indentation after the if-else statement. The following is the correct way of using the if-else statements:
# defining a variable
var = 10
# if else statements
if var > 5:
print("True")
else:
print("False")
As you can see, now there is proper indentation and when we run the code, we do not get any errors. So, make sure that you have put the spaces after the if-else statement.
Poperly using for loop
The same indentation goes to the for loop. Anything that you write inside the for loop should be properly indented. If you will not put ( at least 4 spaces) to each line of code that is inside the for loop, you will get the error.
The following code will raise the error because of incorrect indentation:
# using the for loop
for i in range(10):
print("Hello")
File "/tmp/ipykernel_272756/1622939147.py", line 3
print("Hello")
^
IndentationError: expected an indented block after 'for' statement on line 2
Here is the correct way of using the for loop in Python:
# using the for loop
for i in range(10):
print("Hello")
If you are not familiar with for loop, a for loop in Python is a loop that is used to iterate through a specified range of elements:
Properly indented while loop
Similar to a for loop, a while loop must also be properly indented so that we will not get the error. A while loop is a loop that continues to execute until the specified condition becomes False.
The following is the incorrect while loop which raises the error:
# defining the loop
var = 5
while var > 0:
print("hello")
var=-1
File "/tmp/ipykernel_272756/1690278472.py", line 6
print("hello")
^
IndentationError: expected an indented block after 'while' statement on line 5
The reason for getting this error is that the code inside the while loop is not properly intended.
The following is the correct way to define a while loop:
# defining the loop
var = 5
while var > 0:
print("hello")
var=-1
Notice that this time, the code inside the while loop has spaces before it which makes it clear and clean as well.
Indentation error in switch statements
Similar to the if statement, if you are using the switch statements, you need to take care of the indentation as well.
The following is the incorrect way of defining and using a switch statement:
var =10
# switch statement
match var:
case 1:
print("You can become a web developer.")
case 2:
print("You can become a Data Scientist")
case 3:
print("You can become a backend developer")
File "/tmp/ipykernel_272756/3850726947.py", line 5
case 1:
^
IndentationError: expected an indented block after 'match' statement on line 4
The following is the correct way of using the switch statements: Check full data science and machine learning course for free:
var =10
# switch statement
match var:
case 1:
print("You can become a web developer.")
case 2:
print("You can become a Data Scientist")
case 3:
print("You can become a backend developer")
Make sure that you have to use the proper indentation if you are using the switch statements:
Summary
In this short article, we discussed the reasons for getting IndentationError: expected an indented block error in Python. We know the error occurs because we don’t put spaces or indentations before each line in Python. Here, we came across possible reasons for this problem and we solved the problem: