Indentation Error in Python

If you are a python programmer or learning the language, you would have run into indentation error a couple of times. Did you ever notice when this error is occurring? How to avoid this error all the time? In this tutorial, these questions will be answered clearly.

The very first thing we need to address here is the term indentation. You might already know the meaning of it.

Indentation is all about spaces. Unlike other programming languages like C, C++, and Java, Python does not use curly braces to make a block of code. Python uses spaces instead. It is a way to tell the interpreter that this group of statements belongs to this particular code block.

All the statements at the same distance from the left belong to the same block, and the other statements are different blocks. Here is an example of a clear view:

Example program:

a = int (input ("Enter the value of a: "))

b = int (input ("Enter the value of b: "))

if (a%2 == 0):

    print (a, "is an even number")

    if (b%2 == 0):

        print(b, "is an even number too")

    else:

        print("But, ",b, "is not an even number")

else:

    print(a, "is not an even number")

    if(b%2 == 0):

        print ("But, ",b, "is an even number")

    else:

        print(b, "is also not an even number")

Output:

Enter the value of a: 6

Enter the value of b: 4

6 is an even number

4 is an even number to

Understanding:

It is a simple program to understand the indentation. The program is meant to identify if the first number, a is an even number or not, and then it has to check if b is an even number.

First, we opened the if statement block with the condition to check if a is even. The next print statement is given a tab space from the left. That is the indentation space. It specifies that the print statement belongs to the If block. Then, it is followed by an else block with its indentation. Now, in the next statement, we used the nested-if. The if statement is given the same indentation as it belongs to the outer if, and the next statement now, as it belongs to the if block inside an if block, we need to give 2 indentations of the two ifs'. Hence, we gave two tab spaces.

All the statements that are only in the outer block need 1 space. All those inside the nested blocks need 2 blocks. Here, one if the block has another if block and an else block in it.

It is the same in the next else block with nested if-else.

If we miss an indentation for a statement, it leads to an indentation error. The interpreter either will not recognize which block the statement belongs to, or it might lead to a logical error as it takes the statement into another block it does not suppose to belong in.

The summary line here is that if we make mistakes in giving spaces that specify the indentation of blocks of code, it leads to an indentation error.

Here is a simple example of a scenario with an indentation error:

Let us take the same code as above and make an indentation mistake:

a = int (input ("Enter the value of a: "))

b = int (input ("Enter the value of b: "))

if (a%2 == 0):

    print (a, "is an even number")

    if (b%2 == 0):

    print(b, "is an even number too")

    else:

        print("But, ",b, "is not an even number")

else:

    print(a, "is not an even number")

    if(b%2 == 0):

        print ("But, ",b, "is an even number")

    else:

        print(b, "is also not an even number")

Output:

print(b, "is an even number too")

    ^

IndentationError: expected an indented block.

Understanding:

The print statement belongs in the nested if statement inside the outer if statement. But, we did not give two tab spaces and gave only one. The interpreter considered the print statement belongs to the outer if. This leads to the syntax error for the nested if and does not make sense. Hence, the indentation error is raised.

Common troubles programmers run into:

  • We have two options for indentation spaces. We can use either tabs or spaces to give white space characters. We are not allowed to use both interchangeably. This gives an indentation error.
  • Spaces are preferred to use over tabs.

Example:

def sum (a, b):

            total = a + b

      return total

Output:

File "test.py", line 3

            return total

            ^

TabError: inconsistent use of tabs and spaces in indentation

Understanding:

In the snippet of code, we gave the tab space in the second line of the function definition, while in the third line, we gave spaces that led to inconsistency and an error.

  • Programmers frequently make indentation mistakes while using compound statements – if, if-else, nested if…
  • Also, the indentation in the function definition must be taken care of carefully.

To avoid and overcome:

Python arranges all the statements in the code as blocks. Indentation is the way of doing it. If we run into an indentation error, the line in which we made a mistake will be displayed in the error message so we can correct it. But, sometimes, the error message shows the line which is affected due to the indentation mistake rather than where it occurred. In such cases, we need to go through every single program line.

We have an option of enabling an option that shows us the tabs and whitespaces as dots on the editor. The dots represent spaces. We can check if we missed a dot in any statement and can keep one.

Summary:

In English, a simple mistake in conjunctions in a sentence changes the whole meaning of the sentence, like let us eat, mother, and let us eat mother. In the same way, a little mistake in space changes the whole mechanism of the program. We need to check the program thoroughly.