Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug)

#1
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 Screenshothttps://blog.finxter.com/wp-content/uplo...300x95.png 300w, https://blog.finxter.com/wp-content/uplo...150x48.png 150w" sizes="(max-width: 572px) 100vw, 572px" />

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 Fixhttps://blog.finxter.com/wp-content/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w, https://blog.finxter.com/wp-content/uplo...150x84.jpg 150w" sizes="(max-width: 768px) 100vw, 768px" />

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 Bookhttps://blog.finxter.com/wp-content/uplo...00x300.jpg 200w, https://blog.finxter.com/wp-content/uplo...8x1152.jpg 768w, https://blog.finxter.com/wp-content/uplo...4x1536.jpg 1024w, https://blog.finxter.com/wp-content/uplo...5x2048.jpg 1365w, https://blog.finxter.com/wp-content/uplo...50x225.jpg 150w" sizes="(max-width: 205px) 100vw, 205px" />

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!

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



https://www.sickgaming.net/blog/2020/10/...tupid-bug/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Install PIL/Pillow in Python? A Helpful Illustrated Guide xSicKxBot 0 5 Yesterday, 12:00 AM
Last Post: xSicKxBot
  [Tut] How To Kill A Thread In Python? xSicKxBot 0 7 10-24-2020, 05:40 AM
Last Post: xSicKxBot
  [Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? xSicKxBot 0 15 10-16-2020, 10:30 PM
Last Post: xSicKxBot
  [Tut] Yield Keyword in Python – A Simple Illustrated Guide xSicKxBot 0 20 10-09-2020, 06:13 PM
Last Post: xSicKxBot
  [Tut] Python Reverse List with Slicing — An Illustrated Guide xSicKxBot 0 39 10-02-2020, 04:24 AM
Last Post: xSicKxBot
  [Tut] How to Remove Duplicates From a Python List While Preserving Order? xSicKxBot 0 37 10-01-2020, 02:21 AM
Last Post: xSicKxBot
  [Tut] How To Update A Key In A Dictionary In Python If The Key Doesn’t Exist? xSicKxBot 0 42 09-29-2020, 09:02 PM
Last Post: xSicKxBot
  [Tut] How to Get the Last Element of a Python List? xSicKxBot 0 43 09-28-2020, 02:36 AM
Last Post: xSicKxBot
  [Tut] How To Format A String That Contains Curly Braces In Python? xSicKxBot 0 35 09-25-2020, 07:51 PM
Last Post: xSicKxBot
  [Tut] Python One Line Dictionary xSicKxBot 0 44 09-21-2020, 05:58 PM
Last Post: xSicKxBot

Forum Jump:

Become a Patron!
[-]
Upcoming Events

[-]
Latest Threads
Microsoft - Microsoft cloud strength fue...
Last Post: xSicKxBot
Today 03:37 AM
» Replies: 0
» Views: 0
News - Random: Crash Bandicoot 4 Dev Has...
Last Post: xSicKxBot
Today 03:37 AM
» Replies: 0
» Views: 0
News - Neil Gaiman Announces American Go...
Last Post: xSicKxBot
Today 03:37 AM
» Replies: 0
» Views: 1
[Tut] Freelance Developer LLC — Is It Sm...
Last Post: xSicKxBot
Today 01:41 AM
» Replies: 0
» Views: 1
(Indie Deal) ?FREE Dyna Bomb, Big Quiz E...
Last Post: xSicKxBot
Today 01:40 AM
» Replies: 0
» Views: 2
Codecks Game Development Project Managem...
Last Post: xSicKxBot
Yesterday 09:34 PM
» Replies: 0
» Views: 6
Fedora - Fedora 33 is officially here!
Last Post: xSicKxBot
Yesterday 09:34 PM
» Replies: 0
» Views: 4
News - Cult FMV Shooter Corpse Killer Is...
Last Post: xSicKxBot
Yesterday 09:34 PM
» Replies: 0
» Views: 4
Microsoft - Lobe app aims to make it eas...
Last Post: xSicKxBot
Yesterday 03:08 PM
» Replies: 0
» Views: 4
News - Switch Is No Longer The Only Way ...
Last Post: xSicKxBot
Yesterday 03:07 PM
» Replies: 0
» Views: 4

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020