Python IndentationError: unindent does not match any outer indentation level Solution

Posted in /  

Python IndentationError: unindent does not match any outer indentation level Solution
vinaykhatri

Vinay Khatri
Last updated on October 31, 2024

    In Python, we use indentation to represent a block code. And we can either use tabs or spaces to indent our code. But if we try to use both and give extra or less spacing to the indentation, we receive the error " IndentationError: unindent does not match any outer indentation level ".

    In this Python guide, we will talk about this error and learn why it is raised. We will also discuss an example that demonstrates this error in a Python program.

    Python Problem IndentationError: unindent does not match any outer indentation level

    In Python, we can either use tabs or spaces to indent our code inside the block code statements because Python does not use curly brackets like other programming languages. While writing a program , we should always consider using one technique to indent our code. If we try to use both space and tab to indent our code and mismatch the number of spaces, we will receive the " IndentationError: unindent does not match any outer indentation level " Error.

    Indentation error is very common, but sometimes it becomes very hard to understand and debug. When we mix the tabs with spaces, our code looks properly indented, but because of some invisible extra spaces, the Python interpreter throws the error.

    Some advanced IDEs and code editors highlight the extra space and automatically indented the code properly. The Error statement "IndentationError: unindent does not match any outer indentation level" has two parts separated by a colon : .

    1. IndentationError
    2. unindent does not match any outer indentation level

    1. IndentationError

    IndentationError is a standard Python exception and a subclass to the SyntaxError. It is raised in a Python program when we put incorrect indentation to the block code.

    2. unindent does not match any outer indentation level

    This is the error message that raises along with the IndentationError exception. This message is telling us that we have not properly indented the body of the block code.

    Example

    def add(num1, num2):
         total = num1 + num2   #5 spaces
        return total           #4 spaces
    
    print(add(2,3))

    Output

      File "main.py", line 3
        return total
                    ^
    IndentationError: unindent does not match any outer indentation level

    Break the code

    In the above example, we are getting this error because the first line of add() function body " total = num1 + num2 " has 5 spaces as an indentation, but the second line " return total " has only 4 spaces of indentation. There are no fixed number of spaces for the indentation in Python, but if you have given 5 spaces as indentation to the first line, the complete body must follow the same number of spaces as an indentation.

    In this example Python's first 5 spaces of indentation for the total = num1 + num2 statement, then when it finds only 4 spaces for the return total statement, it threw the error.

    Solution

    To solve the above problem, we need to ensure that we are specifying the same number of spaces or tabs to all the body blocks for code.

    def add(num1, num2):
    	total = num1 + num2
    	return total
    
    print(add(2,3))   #5

    Common Example Scenario

    You will most probably encounter this error when you run a code that you have copied and pasted from the internet. Another reason why this error occurs in a Python program is when we use tabs and spaces both in the same code block to provide indentation to our code. Some code editor comes with a feature where they figure out such behavior of indentation and highlight the indentation space with color.

    Example

    Let's create a Python program that uses tabs and spaces for the indentation.

    def add(num1, num2):
    	total = num1 + num2    #tab space
            return total           #8 spaces
    
    print(add(2,3))

    Output

      File "main.py", line 3
        return total           #8 spaces
                                        ^
    IndentationError: unindent does not match any outer indentation level

    Break the code

    The indentation level of the above code statement looks the same, and just by looking at it, you cannot figure out the error. But if you know the use of tabs and spaces, you can tell that the first statement total = num1 + num2 is tab indented and the second statement return total is spaced indented. And when we try to perform tab and space indentation for the same block of code, we encounter the IndentationError.

    Solution

    To solve the above example, we can either indented all the add() function body statements using tabs or spaces. According to coding practice, we should always consider using a tab for the indentation. We can select the add() function code block untab the spaces with the shift+tab key, then again tab the code block with the tab key.

    def add(num1, num2):
    	total = num1 + num2    #tab space
    	return total           #tab space
    
    print(add(2,3))

    Output

    5

    Wrapping Up!

    In this Python tutorial, we discussed what is IndentationError: unindent does not match any outer indentation level Error. This error occurs in a Python program when we use tabs and spaces to indent statements in the same code body block. Or we miss-match the number of spaces for the indentation. This is a very common error, and many Python IDEs and text editors provide syntax highlight features to debug this error.

    To solve this error, you need to ensure that you are using only tabs to provide indentation to the body block. If you are still getting this error in your Python program, you can 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