Indentation in Python – Indent Error Fix


Table of Content
Cause of Python’s Indentation Error
Indentation Error Fix – #1
Indentation Error Fix – #2

To fix Indent Error in Python, just follow one rule –

You must use the same number of spaces throughout the code block; otherwise, Python will throw Errors.

Indentation in Python is a term that refers to the spaces at the start of a code line.

Whereas indentation is used for readability in other programming languages, it is crucial in Python.

Python uses indentation to denote the beginning of a block of code. All of the code we input is arranged using correct whitespaces. 

Consequently, if you have an indentation error, the entire program will fail to run, and the interpreter will return an error.

Python adheres to the PEP8 whitespace ethics when organizing its code. 

So it is recommended that there be four whitespaces or a Tab space between each iteration, with any alternative failing to do so will return an error.

The cause of Python’s Indentation Error

One of the primary causes of the indentation error is the absence of tabs or whitespaces between lines of code. 

Because Python is a procedural language, if you forget to include tabs or spaces between your lines of code, you will almost certainly get this error. 

While the entire program may run correctly in some situations, the error may occur during execution, pausing operation as a whole.

Several of the most typical causes of an indentation error in Python are listed below:

  • While writing lengthy programs, we may forget to indent the compound statements such as ‘for‘, ‘while‘, and ‘if‘, which results in an indentation error.

It’s is the most common type of mistake we make.

  • If we fail to use user-defined classes, an indentation issue will almost certainly occur.
  • While programming, we inserted an indentation in the incorrect location. 

Because Python adheres to tight requirements for code organization, indentation errors are almost often a result of incorrect indentation placement.

  • While coding, we may have utilized both the tab and space keys. 

While both serve the same goal in theory, when used in the same code, the interpreter becomes confused about which modification to employ and thus returns an error.

Indent Error Fix

Now that you understand why this mistake occurs let us look at some possible solutions.

Solution No. 1

While there is no quick resolution for this issue, one thing to remember when attempting to resolve the python indent error is to go over each line individually and determine which carries the error.

Python organizes its code into blocks, making it easier to discover errors. For instance, let’s look at this code below where we use if statements inside for loop.

Consider the following Python code example.

sumEven=0
sumOdd=0

for number in range(1,6):
    
    if (number % 2) == 0:
        sumEven = sumEven+number
        print("{0} is Even number".format(number)) 

    else:
        sumOdd = sumOdd+number
        print("{0} is Odd number".format(number)) 

print(sumEven)
print(sumOdd)

The image below shows a breakdown of the above code into different blocks to understand the indentation in Python in a better manner.

Indentation in Python - Indent Error Fix

We can consider the outer block as Parent Block

In this block, we define global variables, write conditions, make or call functions/methods, run loops, and many more.

We have made a for loop inside the parent block in the above code. Block #1 represents it. 

Inside Block #1, we have Block #2, an if-else condition.

A tab or four blank spaces should precede the lines at Block #2.

The statements under the if condition are represented by Block #3.

The statements under the else condition are represented by Block #4.

Since Block #3 and Block #4 are under one more block, Block #2, the statements of these blocks should be preceded by one more tab or four more blank spaces.

The Output of the code above is –

1 is Odd number
2 is Even number
3 is Odd number
4 is Even number
5 is Odd number
6
9

We can understand the code structure considering Indentation in Python from the image below.

Indentation blocks arrangement - python

If we don’t provide the required tabs or spaces for the statements in that block, Python throws us an Indentation Error. Let’s understand Indentation Error better with the example code below –

sumEven=0
sumOdd=0

for number in range(1,6):

    if (number % 2) == 0:
        sumEven = sumEven+number
        print("{0} is Even number".format(number)) 

    else:
    sumOdd = sumOdd+number
    print("{0} is Odd number".format(number)) 

print(sumEven)
print(sumOdd)

The Output will be –

line 11
    sumOdd = sumOdd+number
    ^
IndentationError: expected an indented block

Solution No. 2

Follow these steps if the technique above does not work for you, and you are having difficulty determining which line you missed the indentation on.

Enable the option to display tabs and whitespaces in your code editor’s settings

When this feature is enabled, you will notice single small dots interspersed throughout your code, each dot representing whitespace or a tab.

If you notice that a dot is missing from a location where it should be, that line most likely contains an indentation error.

Consider the image below to allow the option to display tabs or whitespaces in Visual Studio Code Editor

Python Indentation - view menu - vs code editor - 2

Hence the blank spaces will be denoted as tiny dots in VS Code Editor.

Python Indentation - dots - vs code editor - 1

Consider the image below to allow the option to display tabs or whitespaces in Notepad++ Editor

Python Indentation - view menu - notepad ++ - 3

Hence the blank spaces will be denoted as tiny dots in Notepad++ Editor.

Python Indentation - dots - notepad ++ - 4

Another way to resolve this issue is to use the Python interpreter’s built-in Indent Guide.

Enabling this option will walk you through each line of code and highlight the location of your error. 

Although this method is inefficient when considering that your software has multiple lines of code, it is the only way to ensure that all existing problems are identified and corrected.

Conclusion

One of the most intriguing questions from coders of all expertise and experience is how to solve the indentation error in Python.

Regardless of the experience level with programming, this happens to everyone at some point or another.

Python indentation beautifies our code. 

Additionally, it aids in grouping the statements into a code block. As a result, a constant practice of developing beautiful code is not a Nice-To-Have feature but a Must-Have necessity of the code.

By implementing the techniques listed above, you may resolve your Python indentation error and continue programming efficiently in the future.

You may like to Explore –

Python: How to Check If Key Exists In Dictionary?

Python File close() Method – How to close a file in Python?

Cheers!

Happy Coding.

About the Author

This article was authored by Rawnak.

2 thoughts on “Indentation in Python – Indent Error Fix”

Comments are closed.