Python IndentationError: unexpected indent (How to Fix This Stupid Bug)

If you’re like me, you try things first in your code and fix the bugs as they come. One frequent bug in Python is the IndentationError: unexpected indent. So, what does this error message mean?

The error IndentationError: unexpected indent arises if you use inconsistent indentation of tabs or whitespaces for indented code blocks such as the if block and the for loop. For example, Python will throw an indentation error, if you use a for loop with three whitespace characters indentation for the first line, and one tab character indentation of the second line of the loop body. To fix the error, use the same number of empty whitespaces for all indented code blocks.

Python Indentation Error Message Screenshot

Let’s have a look at an example where this error arises:

for i in range(10):
  print(i)
   print('--')

The first line in the loop body uses two whitespaces as indentation level. The second line in the loop body uses three whitespace characters as indentation level. Thus, the indentation blocks are different for different lines of the same block. However, Python expects that all indented lines have structurally the same indentation.

How to Fix Python’s Indentation Error?

To fix the error, simply use the same number of whitespaces for each line of code:

for i in range(10):
    print(i)
    print('--')

The general recommendation is to use four single whitespace characters ' ' for each indentation level. If you have nested indentation levels, this means that the second indentation level has 4+4=8 single whitespace characters:

for i in range(10):
    for j in range(10):
        print(i, j)

Mixing Tabs and Whitespace Characters Often Causes The Error

A common problem is also that the indentation seems to be consistent—while it really isn’t. The following code has one tab character in the first line and four empty whitespaces in the second line of the indented code block. They look the same but Python still throws the indentation error.

Python Indentation Error How to Fix

On first sight the indentation looks the same. However, if you go over the whitespaces before print(i), you see that it consists only of a single tabular character while the whitespaces before the print(j) statement consists of a number of empty spaces ' '.

Try It Yourself: Before I tell you what to do about it, try to fix the code yourself in our interactive Python shell:

Exercise: Fix the code in the interactive code shell to get rid of the error message.

Do you want to develop the skills of a well-rounded Python professional—while getting paid in the process? Become a Python freelancer and order your book Leaving the Rat Race with Python on Amazon (Kindle/Print)!

Leaving the Rat Race with Python Book

How to Fix The Indentation Error for All Times?

The source of the error is often the misuse of tabs and whitespace characters. In many code editors, you can set the tab character to a fixed number of whitespace characters. This way, you essentially never use the tabular character itself. For example, if you have the sublime text editor, the following quick tutorial will ensure that you never run in this error ever again:

  • Set Sublime Text to use tabs for indentation: View –> Indentation –> Convert Indentation to Tabs
  • Uncheck option Indent Using Spaces in the same sub-menu above.

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!