IndentationError: expected an indented block

The IndentationError: expected an indented block occurs when you don’t have enough indents added at the beginning of the line. In python, the compound statement and functions requires the indent to be distinguished from other lines. The editors such as sublime, BBEdit, Visual Studio Code will do auto indent.

The indent is defined as the distance or number of empty spaces between the start of the line and the left margin of the line. Indents are not considered in the most recent programming languages such as java, c++, dot net, etc. Python uses the indent to distinguish compound statements and user defined functions from other lines.

The error message IndentationError: expected an indented block indicates that there is a lack of indent in the line that the python interpreter expects to have. The indentation error will be thrown as below.

File "/Users/python/Desktop/test.py", line 5
    print "hello world";
        ^
IndentationError: expected an indented block
[Finished in 0.0s with exit code 1]


Root Cause

Python is the sentivite language of indentation. Compound statement and functions require an indent before starting a line. The error message “IndentationError: expected and indented block” is thrown due to a lack of indent in the line that the python interpreter expects to have.

There’s no syntax or semantic error in your code. This error is due to the style of writing of the program



Solution 1

In most cases, this error would be triggered by a mixed use of spaces and tabs. Check the space for the program indentation and the tabs. Follow any kind of indentation. The most recent python IDEs support converting the tab to space and space to tabs. Stick to whatever format you want to use. This is going to solve the error.

Check the option in your python IDE to convert the tab to space and convert the tab to space or the tab to space to correct the error.



Solution 2

In the sublime Text Editor, open the python program. Select the full program by clicking on Cntr + A. The entire python code and the white spaces will be selected together. The tab key is displayed as continuous lines, and the spaces are displayed as dots in the program. Stick to any format you wish to use, either on the tab or in space. Change the rest to make uniform format. This will solve the error.

Program

a=10;
b=20;
if a > b:
	print "Hello World";      ----> Indent with tab
        print "end of program";    ----> Indent with spaces

Solution

a=10;
b=20;
if a > b:
	print "Hello World";      ----> Indent with tab
	print "end of program";    ----> Indent with tab


Solution 3

The program has no indentation where the python interpreter expects the indentation to have. The blocks are supposed to have an indentation before the beginning of the line. An indentation should be added in line 4 in the example below

Program

a=20;
b=10;
if a > b:
print "hello world";

Output

File "/Users/python/Desktop/test.py", line 5
    print "hello world";
        ^
IndentationError: expected an indented block

Solution

a=20;
b=10;
if a > b:
	print "hello world";

Output

hello world
[Finished in 0.0s]


Solution 4

Python may have an incomplete block of statements. There may be a missing statement in the block. Some of the lines may be incomplete or deleted from the program. This is going to throw the indentation error.

Add missing lines to the program or complete the pending programming. This is going to solve the error.

program

a=20;
b=10;
if a > b:
	print "hello world";
else:

Solution

a=20;
b=10;
if a > b:
	print "hello world";
else:
	print "hello world in else block";

Output

hello world
[Finished in 0.0s]


Solution 5

In the above program, if the else block is irrelevant to logic, remove the else block. This will solve the indent error. The Python interpreter helps to correct the code. Unnecessary code must be removed in the code.

Program

a=20;
b=10;
if a > b:
	print "hello world";
else:

Output

File "/Users/python/Desktop/test.py", line 5
    print "hello world";
        ^
IndentationError: expected an indented block

Solution

a=20;
b=10;
if a > b:
	print "hello world";

Output

hello world
[Finished in 0.0s]


Solution 6

In the python program, check the indentation of compound statements and user defined functions. Following the indentation is a tedious job in the source code. Python provides a solution for the indentation error line to identify. To find out the problem run the python command below. The Python command shows the actual issue.

Command

python -m tabnanny test.py 

Example

$ python -m tabnanny test.py 
'test.py': Indentation Error: unindent does not match any outer indentation level (<tokenize>, line 3)
$ 


Solution 7

There is an another way to identify the indentation error. Open the command prompt in Windows OS or terminal command line window on Linux or Mac, and start the python. The help command shows the error of the python program.

Command

$python
>>>help("test.py")

Example

$ python
Python 2.7.16 (default, Dec  3 2019, 07:02:07) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help("test.py")
problem in test - <type 'exceptions.IndentationError'>: unindent does not match any outer indentation level (test.py, line 3)

>>> 
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> ^D



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *