Expected an Indented Block Error in Python

A Quick Answer

This error occurs when Python expects an indented block and doesn’t find it. For example:

a = 7
if a == 7:
print(a)

Output:

line 3
print(a)
^
IndentationError: expected an indented block

Solution

The error log shows where the indentation problem is arising from. In the above case, it is in line 3. If we indent that line as shown below, the problem is solved.

a = 7
if a == 7:
	print(a)

Output (no IndentationError):

7

IndentationError can also occur in other circumstances. The rest of the article covers those circumstances and explains the purpose of indentation in Python.

Indentation in Python

Indentation refers to adding white spaces before a code statement. In Python, indentation is used to group code statements into blocks. A programming language that allows code grouping is called a block-structured language – Python is one of such languages.

A block in a programming/ scripting language is used to limit the lexical scope of a variable, function, or class. In some other cases, however, grouping code statements serve the purpose of improving the readability of the code.

Different programming or scripting languages use different methods of grouping code. For example,

Programming Languages Code Blocking
Basic and Fortran No way of explicitly using block structures
ALGOL, Pascal begin…end
Bourne and Bash Shell if…fi, do…done
C, C++, Perl, Java, and many more { }
Python Indentation
Table 1: Code blocking for different programming languages

As shown in the above table, Python operates on a distinct tenet. Python code is organized using indentation; that is, the indentation of code blocks defines those blocks.

In Python language, indentation is a requirement, not a formatting style. This idea makes Python code naturally more readable.

How indentation is done in Python

All statements with the same distance to the right belong to the same block of code, i.e., the statements within a block line up vertically.

The block ends at a line less indented or at the end of the file. If a block has to be more deeply nested, it is simply indented further to the right (see block 3 in Figure 1 below ).

Figure 1 : Left: The code. Right: Block structures delimited by indentation.

Python expects indentation for code structures like classes, functions, if-statement, while, with, for-loop, and try…except for error handling exceptions.

Note: Indentation, in Python, can be done using white spaces from the space bar or the tab key. An indent must be at least one white space or a tab space. A consistent indentation length must be used for all the code blocks in a file.

The Causes and the Solutions to Python “Expected an Indented Block” Error

IndentationError often happens because of the following three reasons:

Reason 1: Not using Indent when it is needed

This error occurs when Python expects an indented block for a given code statement, but it is not given. For example;

a = 7
for i in range(10):
	if i == a:
	print(f"Number {i} found.")

Output:

line 4
print(f"Number {i} found.")
^
IndentationError: expected an indented block

In the code snippet above, Python excepts indentation under if-statement, which is shown clearly on the error log.

Solution

Check your code to ensure that you use indentation spaces when needed. In most cases, Python will show you the code line causing the problem. In the above example, we need to indent the print statement in line 4 as follows:

a = 7
for i in range(10):
	if i == a:
    		print(f"Number {i} found.")

Output:

Number 7 found.

Reason 2: using indentation spaces of different sizes

Another form of IndentationError can arise because of using indentation spaces of inconsistent sizes. Here is an example (based on the code editor you are using the error thrown here might match the error explained in the next section):

a = 7
for i in range(10):
  		 if i == a:
		  print(f"Number {i} found.")

Output:

IndentationError: unindent does not match any outer indentation level

The error produced is caused by using different sizes of indentation space. The block under the for-loop is 2 tabs, whereas the block under the if-statement is 2 tabs and 1 space.

Solution

The code blocks of the same level must be the same distance from the left margin. In the above code, for example, we can solve the problem by using a tab to indent the for-statement as block 1 and 2 tabs to indent block 2 (the if-statement).


Reason 3: Inconsistent use of tabs and spaces in indentation

Python code blocks must be indented using either tabs or white spaces – do not mix the two in one file. This indentation problem might be difficult to identify at a glance because the length of a tab and 4 spaces, for example, might look the same – but they are not. Here is an example,

a = 7
for i in range(10):
  	 if i == a:
		 print(f"Number {i} found.")

Output:

line 4
TabError: inconsistent use of tabs and spaces in indentation

Python complaints about using tabs and spaces in our indentation. For for-loop, I used a single tab, and under if-statement, I used 4 spaces. According to the Python interpreter, that is inconsistent.

Note: Python marks this error as a TabError and not IndentationError.

Solution

Check the problematic line and use an indentation matching what you used in the preceding lines.

To identify the discrepancy, you can search for white space in your code and, in so doing, be able to locate the problematic line(s). For most editors, this can be achieved by opening a search bar with Ctrl+F, hitting the space bar, and then Enter.

Conclusion

Indentation in Python is a language requirement – it is not used to improve the readability of code only. For that reason, indentation must be done correctly; otherwise, Python will throw an error.

In this article, we discussed three reasons that cause the IndentationError – missing indentation, using indent spaces of different sizes, and mixing tabs and spaces for indentation. We also covered the solution to such errors.