How to Fix Indentation Error in Python

In this tutorial, we will show you the steps to fix the Indentation Error in Python. In programming languages, an indentation is a set of spaces before the start of a line to signify that its the start if a new para. This is mostly used in the case of ‘For’, ‘If’, and other related loops, thereby giving it a structured outlook and allowing the interpreter to execute the code. However, if you end up missing these whitespaces, then it might be a cause of issues.

Likewise, if the number of spaces isn’t equally distributed, then it might contribute to the error as well. In this regard, Python follows the PEP8 whitespace rule so it is generally recommended to have four whitespaces and no hard-tab characters. But if you don’t follow these rules, then you might get stuck with indentation issues. If that happens, then you could take the help of this guide to fix the Indentation Error in Python. Follow along for the complete instructions.

Fix Indentation Error Python

Why Indentation Error Happens

While giving out spaces, some people generally mix up the Space bar as well as the Tab key. While both could be used for this purpose, but in Python, you have to select one and stick with it. If you use both of them interchangeably, then the Interpreter might not be able to distinguish between the whitespaces. Furthermore, if you are using For, If and other such loops, make sure to follow the correct indentation patters.

python block indentation

While using these nested loops, unequal indentation seems to be the topmost reason for the error. See the above example as a reference. The first block has the second block which in turn contains the third block. After this, we continue the second block and then the first one. You are able to distinguish between all these because there is an equal indentation between these nested blocks. So always follow this rule while indenting these loops.

Advertisements

How to Fix the Indentation Error in Python

To begin with, whenever dealing with the For and If blocks, follow the right indentation pattern with equal spacing. And if you are using Tab for whitespaces, then continue with that only, don’t mix it up with  Spacebar. For example, in the below image, see how we have nested the If block inside of For and then ended both the loops. You should also follow a similar pattern and this should fix the Indentation Error in Python.

loop

Along the same lines, your code editor might have the option to display all the tabs and whitespaces. It is recommended that you enable it before proceeding ahead with coding. For example, if you are using Notepad++, then head over to the View tab and go to the Show Symbol option. Then tick-mark the Show White Space and TAB option.

show space notepad

Advertisements

Now, enabling this feature will then place single dots for every space. This will give you an idea as to the number of whitespaces you have entered before each line. In the below screenshot, the red sections signify three spaces with three single dots, while the green has four single dots (four spaces). This will help you in clearly indenting each block and hence should fix the issue as well.python indentation notepad

So this was all from this guide on how to fix the Indentation Error in Python. The above-mentioned methods should be able to rectify the said issue. If you still have any queries concerning the instructions, do let us know in the comments. Rounding off, here are some iPhone Tips and TricksPC tips and tricks, and Android Tips and Trick that you should check out as well.

Comments

  1. Hi; I came across your site and bookmarked it and I come back every now and then to seek some advice: so far every idea or instructions for fixing conflicts have been excellent and the info was made very clear(since I’m not computer knowledgeable) and concise. Thank you

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.