Skip to content

How to Fix: Python indentationerror: unindent does not match any outer indentation level Solution

How to Fix Python indentationerror unindent does not match any outer indentation level Solution Cover Image

In this tutorial, you’ll learn how to fix a common error you’ll encounter in Python, the indentation error. Indenting your code is an important function for writing useful programs. However, indenting code improperly will raise the following error: “Python indentationerror: unindent does not match any outer indentation level”.

By the end of this tutorial, you’ll have learned:

  • How to fix the Python indentation error
  • How indentation works in Python and why it matters
  • How to use popular IDEs, such as VS Code, to easily resolve the Python indentation error

Let’s get started!

What is the Python Indentation Error?

The Python indentationerror is caused by a number of different factors:

  • Mixing spaces and tabs in indentation
  • Mismatched levels of indentation in code blocks

Both of these scenarios will cause the indentationerror to be raised. Unfortunately, it’s not immediately clear which of these scenarios caused the error to happen. Before we dive into further details, let’s explore how indentation works in Python.

How Indentation Works in Python

Python allows you to indent your code with either spaces or tabs. It doesn’t matter which one you choose – however it requires you to be consistent in that choice. Because Python is a statically typed language, Python will not be able to work properly if spaces and tabs are both used for indentation.

In fact, when copying code from the internet, this is a common error you’ll encounter. Throughout the rest of this tutorial, you’ll learn how to identify and resolve this error.

Should You Indent with Spaces or Tabs in Python?

While it doesn’t matter whether you use tabs or spaces to indent your code, Python does provide some guidance as to which methods are preferred. The PEP-8 standard on indenting your code indicates that using four spaces per indentation level is the preferred method of indenting your code.

The PEP standard goes even further. It says that spaces are preferred and that tabs should only be used to remain consistent with existing code.

Causes of the Python Indentation Error

Let’s explore the causes of the Python indentationerror a little further. Earlier, you learned that the error is caused by mixing spaces and tabs in indenting code as well as having improperly indented code.

Let’s explore both of these in further detail in order to see how the error can be resolved.

Mixing Spaces and Tabs in Python

One of the most common causes of the Python indentationerror is mixing tabs and spaces in your code when indenting. The problem with this is that it’s often not intuitive to find. This problem mostly occurs when you copy and paste code from another source into a Python program.

Thankfully, most modern IDEs will account for this and automatically convert tabs into spaces. For example, Visual Studio Code will, by default, convert tabs into the corresponding number of spaces. Because of this, this error is more common when you’re running Python programs in other applications.

In many cases, you’ll be able to select tabs using find and replace by searching for the \t string.

Incorrect Indenting in Python

The other cause of the Python indentationerror is having incorrect indenting in your Python code. This can occur when you’re writing any blocked type pieces of code, such as if-else statements or for loops.

Let’s take a look at an example that would throw a Python indetationerror:

# This code will throw a Python indentation error
for i in range(10):
print(i)

You can fix this easily by indenting the second like of code, as shown below:

# Fixing a Python indentationerror
for i in range(10):
    print(i)

Being mindful of where your code needs to be indented is an important step in writing error-free code. Generally, following a colon, your code will need to be indented.

Conclusion

In this tutorial, you learned how to solve the Python indentationerror. You learned what causes the Python indentationerror and how to solve its two main causes. First, we looked at how the error is thrown when your code mixes both spaces and tabs. Then, we looked at missing or erroneous indentations of your code.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

1 thought on “How to Fix: Python indentationerror: unindent does not match any outer indentation level Solution”

Leave a Reply

Your email address will not be published. Required fields are marked *