Updated April 10, 2023
Introduction to Indentation Error in Python
Python is a language and is used for varied applications. Python is a language where the code is arranged through whitespaces. If there is an incorrect indentation, this will result in an error, and the python interpreter will just return an error function. It uses the PEP8 whitespace ethics. There should be 4 whitespaces used between any alternative or iteration. It happens mainly because of the absence of tabs and whitespaces between the lines of code. If there are any missing spaces between the code, you will face this error as Python is a procedural language.
Syntax:
There is no particular syntax for indentation. Instead of braces that are used in other languages, Python uses indentation. A block of code should start with indentation, and the ending of the code should be first-line which is not indented. The common practice is of adding four whitespaces which are usually preferred over tabs.
Example:
for i in range(1,24):
print(i)
if i == 8:
break
The spaces above will work properly as indentation is proper in the code, and it will run successfully.
How does Indentation Error work in Python?
Python is a procedural language. The indentation error can occur when the spaces or tabs are not placed properly. There will not be an issue if the interpreter does not find any issues with the spaces or tabs. If there is an error due to indentation, it will come in between the execution and can be a show stopper. You would be curious about what exactly causes these issues with indentation. Below are few reasons due to which this issue can happen.
- The programmer is making use of both spaces and tabs in the code written. This creates ambiguity, and the interpreter is unable to determine which item we need to use in the first place as both are being used interchangeably.
- The programmer missed upon indentation for compound statements like if, for, while, etc.
- This is the most basic need when using Python. If the indentation technique is not practiced, then you will see this error every now and then.
- It is very important that this indentation practice is used even when the programmer is working with user-defined functions or different classes.
The above are the usual cases where the user faces indentation errors while programming with Python. We will see a few examples of these errors ahead and also how to avoid them.
Examples of Indentation Error in Python
Let us see some examples where indentation error occurs and how we can resolve them.
Example #1
Code:
site = 'edu'
if site == 'edu':
print('Logging in to EduCBA!')
else:
print('Please type the URL again.')
print('You are ready to go!')
This is a simple program where we are using the if-else block. Here we have declared a variable site that has the value. It has the value edu. We make use of if to check if the value in the site variable is edu, then it will display the desired message. If the variable is not the same, then the statement following else block will be displayed. Now syntactically, the above program is correct, and there is no error in the above code. But id you see the below screenshot, you will see an error has occurred.
You can clearly see there is an indentation error that has occurred. In order to overcome this error, we will have to use proper spacing for the print statement that follows the if block. The new code should be like the below in order to make the code work.
Code:
site = 'edu'
if site == 'edu':
print('Logging in to EduCBA!')
else:
print('Please type the URL again.')
print('You are ready to go!')
When the above code is run with proper whitespace, the desired output is received. The if block now runs successfully, and the output is received as expected.
Example #2
Code:
j = 1
while(j<= 5):
print(j)
j = j + 1
The above code is a loop where the program should display numbers from 1 to 5. Here if you see the code, there is no alignment of spaces after the print(j) function. This leads to misplaced whitespaces. Here the code may or may not even throw an error. The interpreter just keeps running, or the output may be a blank screen. The output will be in either of the two ways.
It will either throw the above error or maybe just keep running. The correct way of writing this code is as below:
j = 1
while(j<= 5):
print(j)
j = j + 1
The above code will run without throwing any errors and give the desired output. In python, we do not make use of braces, and hence indentation comes into the picture. Indentation plays an important role to keep the code systematic.
As the indentation is correct in the above program, the output is as expected in the above screenshot.
How to Avoid Indentation Error in Python?
In order to not have an indentation error in Python, you must go through every line of your code separately. This will help you in understanding which line contains the error or does not have the expected whitespaces. Python arranges all lines of codes in the form of blocks. Hence just like there should be braces for the module, here should be spaces. You can just think that the braces are invisible, but they should be there in the form of spaces.
Another way of not facing this error is that you can simply go to your editor and enable the option to show the tabs and whitespaces. After this option is enabled, you can see dots on the editor where the dot act as space. If the dot is not being seen, then probably you have just generated an indentation error.
Conclusion
Python is a very user-friendly language. It mainly is similar to English and hence is being used widely. It makes use of whitespaces and tabs for keeping the code systematic. Like other languages, there is no need for any braces in Python. If these spaces are missed or misplaced, then an Indentation error occurs. It can be easily resolved by having proper spaces in the code.
Recommended Articles
This is a guide to Indentation Error in Python. Here we discuss the working of Indentation Error and how to avoid it in Python, along with different examples and its code implementation. You can also go through our other related articles to learn more –