Python IndentationError: expected an indented block Solution

Posted in /  

Python IndentationError: expected an indented block Solution
vinaykhatri

Vinay Khatri
Last updated on October 31, 2024

    Like other programming languages Python also follow a strict syntax to write the code. In Python, we do not have the curly brackets to represent the block code. Instead, we use the indentation. This indentation syntax is mandatory and provides a better and legible way to write code in Python. While writing the block code body in Python, if we do not indent the code properly, there we would receive the " IndentationError: expected an indented block " error.

    This Python guide will walk you through Python's IndentationError: expected an indented block in detail. This article also covers some common example scenarios that show how to debug a Python program error. So let's get started with the Error statement.

    Python Error "IndentationError: expected an indented block"

    In other programming languages like C, C++, Java, and JavaScript, we only use indentation for code legibility, and there we have curly brackets to represent block code for functions, if..else, class, etc., body. In Python, we use indentation when we want to write the body or block code for functions, if..else, and class statements. And there, if we do not intend the code properly, we will encounter the error "IndentationError: expected an indented block". This error statement has two sub statements

    1. IndentationError
    2. expected an indented block

    1. IndentationError

    IndentationError is one of the Python exceptions, it is raised by the Python interpreter when we do not indent the code for a block statement.

    2. expected an indented block

    This statement is the error message, telling us that we have not properly indented the block code body.

    error example

    def total(bill):
    return sum(bill)
    
    bill = [282, 393, 3834, 888, 9373,767]
    
    print("Total is:", total(bill))

    Output

     File "main.py", line 2
    return sum(bill)
    ^
    IndentationError: expected an indented block

    Break the code

    In the first line of our code, we have defined a function using the def keyword, and in the second line, we have the return statement that is not indented for the total(bill) function definition. That's why Python is throwing the IndentationError: expected an indented block error. Even in the error output statement, Python is showing the error message that return sum(bill) is expected to indent for the function body.

    Solution

    To solve the above problem, we need to indent the return statement inside the function body.

    def total(bill):
        return sum(bill)
    
    bill = [282, 393, 3834, 888, 9373,767]
    
    print("Total is:", total(bill))

    Output

    Total is: 15537

    Common Example Scenario

    We only receive this error in our program when we do not indent a single line of code for the block code statements.

    Error Example

    Suppose we have a list of numbers, and we need to write a function that accepts that list and returns two lists, even and odd. Where even contain only even numbers, and the old list contains odd numbers from the numbers list.

    def even_odd(numbers):
        odd = list()
        even = list()
        
        for num in numbers:
            #check for the odd and even numbers
        if num%2==0:  #error
            even.append(num)
        else:
            odd.append(num)
    
        return even, odd
    
    numbers = [97, 60, 33, 39, 54, 87, 27, 99, 32, 94, 69, 42, 83, 20, 36, 34, 62]
    
    even, odd = even_odd(numbers)
    
    print("ODD:", odd)
    print("EVEN:", even)

    Output

      File "main.py", line 7
        if num%2==0:
        ^
    IndentationError: expected an indented block

    Break the Error

    In this example, we are receiving the error in line 7 with the if num%2==0: statement, which you can also see in the output. In the above program, we have not indented any code for the for loop that's why we are getting the error. And the error output statement shows us that the if num%2==0: is expected to be indented for that for loop.

    Solution

    To solve the above problem, all we need to do is indent the if logic code for the for loop statement so that the logic can iterate multiple times.

    def even_odd(numbers):
        odd = list()
        even = list()
        
        for num in numbers:
            #check for the odd and even numbers
            if num%2==0: 
                even.append(num)
            else:
                odd.append(num)
    
        return even, odd
    
    numbers = [97, 60, 33, 39, 54, 87, 27, 99, 32, 94, 69, 42, 83, 20, 36, 34, 62]
    
    even, odd = even_odd(numbers)
    
    print("ODD:", odd)
    print("EVEN:", even)

    Output

    ODD: [97, 33, 39, 87, 27, 99, 69, 83]
    EVEN: [60, 54, 32, 94, 42, 20, 36, 34, 62]

    With this, now our code runs successfully.

    Conclusion

    The IndentationError: expected an indented block is a very common error. If you are using an advanced Python IDE or text editor, there you get the auto-indentation feature that minimizes this error. Still, if you are getting this error in your output, all you need to do is go for the error line and check if the block code is properly indented or not. Now you know how to solve this error for your Python program.

    If you are still stuck in this error, please share your code and query in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments