How to Solve expected an indented block error in Python

0
3949
How to Solve Python expected an indented block Error

Not all programming languages are measured on the Indentation, but Python is a different language. Python uses indentation to decide which lines of code belong to other lines.

Indentation uses spaces or tabs to separate the code for better visualization. So let’s see how to resolve the Indentation error.

Python expected an indented block

The IndentationErrorexpected an indented block error occurs in Python when the code is not indented correctly. To solve an expected indented block error in Python, correct the indentation of each block. In Python, Indentation is part of the syntax.

Before Python runs any code in your program, it will first discover each line’s correct parent and children. Then, python throws an Indentation whenever it comes across a line for which it cannot define the right parent to assign.

def compare(num):
    if num >= 1:
    print("It is positive number")
    elif num < 0:
    print("It is negative number")
    else:
    print("It is zero")  


compare(1)

If you run the above file, your output looks like the one below.

 File "/Users/krunal/Desktop/code/pyt/database/app.py", line 3
    print("It is positive number")
    ^
IndentationError: expected an indented block

To resolve this error, check if you have left alone an elif: part of an if-condition, and check if the indentation is missing after conditions, loops, etc.

You can see in our example that there should be an indentation before starting a new statement after the if block, elif block, and else block.

Still, we did not put any whitespace, and that caused an indentation error. So let’s resolve the error by providing whitespaces.

def compare(num):
    if num >= 1:
        print("It is positive number")
    elif num < 0:
        print("It is negative number")
    else:
        print("It is zero")


compare(1)

Output

It is positive number

And the IndentationError is successfully resolved.

For example, Python statements start with def or must have at least one child. This means that a function must have at least one line of code. It also means that a conditional must have at least one line of code to run if the condition is True.

Let’s see the example in which we don’t write anything after the first if statement.

def compare(num):
    if num >= 1:
      
    elif num < 0:
        print("It is negative number")
    else:
        print("It is zero")


compare(1)

Output

File "/Users/krunal/Desktop/code/pyt/database/app.py", line 4
    elif num < 0:
    ^
IndentationError: expected an indented block

After Python reads the if statement, it expects to see at least one child line following it. However, since the next non-empty line reads it is the elif statement, which means the if statement has no children, Python reports that it expected some indented lines.

To fix this IndentationError, either place at least one line of code as the if statement’s child or remove them entirely.

def compare(num):
    if num >= 1:
        print("It is positive number")
    elif num < 0:
        print("It is negative number")
    else:
        print("It is zero")


compare(1)

If you run the above code, you will get the expected output.

Python unexpected indent

Python throws an IndentationError when it finds a line indented as if the line had some parent line, but it couldn’t get any lines above to be its parent.

For example, you face this unexpected indent error when a line is indented by one or more spaces more than the previous line, and the previous line isn’t def, if, elif, else, for, or while loop.

That is it for IndentationError in Python and how to resolve it.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.