Select Page

How to Solve Python IndentationError: unindent does not match any outer indentation level

by | Programming, Python, Tips

Python uses indentation to define the scope and extent of code blocks in constructs like class, function, conditional statements and loops. You can use both spaces and tabs to indent your code, and if you use both methods when writing your code, you will raise the error: IndentationError: unindent does not match any outer indentation level.

We will go through the error in detail and an example to learn how to solve it.


IndentationError: unindent does not match any outer indentation level

What is Indentation in Python?

Indentation is vital for constructing programs in Python. It refers to the correct use of white space to start a code block. With indentations, we can quickly identify the beginning and endpoint of any code block in our program. Let’s look at how indentation in Python works visually:

code indentation in Python
Code indentation in Python

To indicate a code block in Python, you must indent each block line by the same amount. You can use four spaces or one tab, a typical indentation for Python. According to the conventions in PEP 8, four white spaces is preferable. You can use indentation to nest code blocks within code blocks.

Python objects if you use both spaces and tabs to indent your code. You need to use one form of indentation, and this can be tricky because you cannot see the difference between spaces and tabs.

The error commonly occurs when you copy code from other sources to your script. The code you are copying may have a different indentation to what you are using.

The error can also occur if you have used indentation in the wrong place or have not used any indentation.

Example: Mixing Indentation in Function

Let’s write a program that calculates the square roots of a list of numbers and prints the result to the console. We will start by defining the function to calculate the square root of a number:

def get_square_roots(number_list):

    for number in number_list:

        sqrt_number = number ** 0.5

	    print(f'The square root of {number} is {sqrt_number}')

The function uses a for loop to iterate through every number in the list you will pass. We use the exponentiation operator to calculate the square root of the number and then print the result. Next, we will define the list of numbers and then call the get_square_roots() function.

number_list = [4, 9, 16, 25, 36]

get_square_roots(number_list)

Let’s run the code and see what happens:

sqrt_number = number ** 0.5
                          ^
IndentationError: unindent does not match any outer indentation level

The code returns the IndentationError, and the tick indicates the line responsible for the error.

Solution

We can use a text editor like Sublime Text to see the indentation style in our code by highlighting it, as shown below.

Code block with mixed indentation style
Code block with mixed indentation style

Each line represents a tab, and a dot represents a space. We can see a mix of spaces and tabs in the code snippet, particularly the line sqrt_number = number ** 0.5. To fix this, you can change replace the indentation on the other lines with four white spaces as this is the preferred indentation method. Alternatively, you can use tabs. Let’s look at the revised code in the text editor:

Code block with consistent indentation style
Code block with consistent indentation style

We can see each line has spaces instead of tabs. Let’s run the code to see what happens:

The square root of 4 is 2.0
The square root of 9 is 3.0
The square root of 16 is 4.0
The square root of 25 is 5.0
The square root of 36 is 6.0

The program returns the square root of each number in the list we pass to the function. You do not need to use a text editor to find differences in indentation style, but it does make it easier to spot them. Alternatively, you can manually go through each line in your code and stick to one indentation style.

Summary

Congratulations on reading to the end of this tutorial! The error “indentationerror: unindent does not match any outer indentation level” occurs when you mix spaces and tabs to indent your code. To solve this error, ensure all your code uses either spaces or tabs in your program. You can use a text editor to highlight your code to make it easier to spot the differences. Otherwise, you can go over the lines in your code and stick to one indentation style. To minimise the likelihood of this error, you can write your code in a text editor or Integrated Development Environment (IDE) like PyCharm, which automatically indent your code in a consistent style.

For further reading on Python code structure, go to the article: How to Solve Python UnboundLocalError: local variable referenced before assignment.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!