AppDividend
Latest Code Tutorials

How to Solve Python expected an indented block Error

0

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.

If the code is not indented correctly, you will face this IndentationErrorexpected an indented block. In Python, Indentation is part of the syntax. Let’s see how to resolve the Indentation error.

Python expected an indented block.

To resolve expected an indented block in Python, correct the indentation of each block. Before Python runs any code in your program, it will first discover the correct parent and children of each line. Python throws an Indentation whenever it comes across a line for which it cannot define the right parent to assign.

See the following example where Python throws an IndentationError.

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 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 after if block, elif block, and else block, there should be an indentation before starting a new statement. Still, we did not put any whitespace, and that causes an indentation error. 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 that start with def or if 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 it reads is the elif statement which means the if statement has no children, and 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)

Now, 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

Your email address will not be published.

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