Python expected an indented block

Python is a sensitive language that depends on indentation. Missing a space or adding extra space can cause an expected error and generate an error message. Python requires its code to be correctly indented and …

Python expected an indented block

Python is a sensitive language that depends on indentation. Missing a space or adding extra space can cause an expected error and generate an error message. Python requires its code to be correctly indented and spaced. Never mix space and tab if you are using space. Stick to that or if you are using a tab, then stick to the tab, don’t inter-mix them; that can cause errors because the tab may have a different number of spaces.

What is an indented block?

Indents are the blank space at the start of the statement or from the margin of code. Python does not use brackets [{}] like other programming languages JavaScript and C to indicate the block of code for class and function definition. Usually, space or tabs are used in different programming languages for a better understanding of code or to make it look better. But in python, space or tabs are part of programming; forgetting an indent can cause an expected indentation error.

Ideally, while working on python, indentation is a part of programming. Basically, it is four-space, or we can use a tab. Many software uses indentation (four spaces) at the start of the function statement automatically. Still, when writing python code manually (on notepad), we should take care of the number of spaces or indentations by ourselves. When the function is compound, it means when it contains more than one statement, it will again add indent (four-space) in the second line of defined function means total eight-space in that statement.

Common cause of this error includes:

  • Missing an indent in the statement within a compound statement.
  • Not using indent in a user-defined function statement.

The error message Indentation error: expected an indentation block would indicate that you have an indentation error. 

Let’s check what we said in this article to be accurate.

For this, let’s take a simple example of a python block of code.

In this piece of code, we missed an indent after else statement in line 6. Let’s see what output it gives.

a=10
b=20
if a<b:
    print("a is lesser than b")
else:
print("a is greater than b")

Output:

File "<string>", line 6
    print("a is greater than b")
^
IndentationError: expected an indented block

Hence, it is proved that missing an indent in python can cause the indentation error.

To solve the Indentation Error: expected in indentation block, we add proper indent after else statement.

a=10
b=20
if a<b:
    print("a is lesser than b")
else:
    print("a is greater than b")

Output:

a is lesser than b

let’s take another example to see indentation error: expected indented block more clearly:

Here, in this example, we added an extra indent in line 8 to check how python responds to adding an extra indent.

i=1000
if (i==500):
    print("i is 500")
elif (i==500):
        print("i is 200")
elif (i==800):
        print("i is 800")
         elif(i==1000):
        print("i is 1000")
else:
    print("i is not present")

Output:

File "<string>", line 8
    elif(i==1000):
    ^
IndentationError: unexpected indent

Adding an extra space shows us an indentation error, but this time it’s an unexpected indent.

And we can also rectify this error by removing extra indent from the code. 

Let’s do this:



i=1000
if (i==500):
    print("i is 500")
elif (i==500):
        print("i is 200")
elif (i==800):
        print("i is 800")
elif(i==1000):
        print("i is 1000")
else:
    print("i is not present")

Here, we removed the extra indent, and let’s see the results:

Output:

i is 1000

wow! it is showing us the output.

So, here we just added an example of an unexpected indentation error to make your concept clearer about indentation error, and it could be expected and unexpected.

Conclusion:

“Indentation Error: expected an indented block” is raised because of not adding or missing indents. To fix this error, follow the syntax format of the python language and add proper indents in your code. Indentation error can also be an unexpected indent block too. To solve this, we remove extra space, and it will be solved.

Suggested ReadRemove Brackets from list Python

Leave a Comment