Sick Gaming
[Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) - Printable Version

+- Sick Gaming (https://sickgaming.net)
+-- Forum: Programming (https://sickgaming.net/forum-76.html)
+--- Forum: Python (https://sickgaming.net/forum-83.html)
+--- Thread: [Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) (/thread-97675.html)



[Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) - xSicKxBot - 10-10-2020

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
Code:
IndentationError: unexpected indent
. So, what does this error message mean?

The error
Code:
IndentationError: unexpected indent
arises if you use inconsistent indentation of tabs or whitespaces for indented code blocks such as the
Code:
if
block and the
Code:
for
loop. For example, Python will throw an indentation error, if you use a
Code:
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.



[Image: image-111.png]

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

Code:
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 loopbody 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:

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

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

Code:
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.

[Image: indentationerror-1024x576.jpg]

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

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

https://repl.it/@finxter/indentationError?lite=true

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)!

[Image: final_cover-683x1024.jpg]
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
    Code:
    Sublime Text
    to use tabs for indentation:
    Code:
    View
    –>
    Code:
    Indentation
    –>
    Code:
    Convert Indentation to Tabs

  • Uncheck option
    Code:
    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!

The post Python IndentationError: unexpected indent (How to Fix This Stupid Bug) first appeared on Finxter.