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.
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.
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)!
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.
👨💻 Recommended: Python Indentation Error – Full Guide
Programmer Humor
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.