You would have encountered a lot of errors during the journey of becoming a programmer. TypeError, ValueError, ZeroError and so on. Life without error just sucks, nothing to accomplish and overcome. Here we are going to discuss indentation error. Why it happens? and how to get rid of it? The problems will be explained and solved along with code snippets. Let's dig in!
Indentation is a space left before starting a new line, which indicates that a new paragraph has started. In terms of a Programming World, indentation is used to indicate Scope in a code. Python is one of the top-ranked languages in the world that uses the indentation method. Indentation is used to point out a specific block of code. Programming languages like C++, JAVA don’t use indentation for this purpose instead they use curly braces ( “{ }” ). Indentation’s other main use is to beautify your code and doesn’t make it look messy.
Scope: The scope of code tells the interpreter “ the sequence of code execution” By sequence I mean which code block to execute at first and which code block to execute at last.
Specifically, we are going to use Python language. If you are an avid Programmer you must have encountered this type of an error displaying:
Now there are several reasons why this error appears.
You are using the Tabs and spaces in the same code to make indentations, although your code looks perfect and is expected to work fine still it throws an error.
Another reason is that indentation practice is not followed by the code block, one line is more indented than the other one. In python Conditional statements (if, else, elif), functions ( are created using “def” keyword) and loops (for, while) uses a colon, and after using a colon the upcoming line needs to indented.
Note: An indentation can be of one tab, 4 spaces, even a single space but once the indentation length is set, all lines must be indented accordingly. then your code will work perfectly.
CODE: (indentation of only single space)
OUTPUT:
In the example below, 👇👇 we created a variable “weather_hot” and then used an “if conditional”. We used two print statements but the first with more indentation, which according to the Python interpreter is not legal and thus it throws an indentation error.
CODE:
OUTPUT (Error):
Python follows the PEP8 whitespace Rule, which depicts that each line that is indented must have 4 whitespaces. Now you can also set the indentation size in your code editor we will talk about that later.
When we use tabs and spaces at the same time the interpreter is not able to figure out which line of code to execute or not. Below is another example of when an indentation error occurs.
In this example, we have created a function that will return the factorial of a number
Let me break down this code. On the first line, we used the “def” keyword to create a function, according to PEP8 rule the next line must be indented, this is our first block of code. After this, we created a “for” loop that ends with a colon. Now the next line is our second block of code. But the problem arises when we incorrectly indent line 5 out of the block. Due to wrong indentation Line5 is not present in any of the code blocks . while executing the interpreter gets confused and thus throws an Indentation error.
CODE (ERROR):
OUTPUT (Error):
CODE (with correct indentation):
OUTPUT:
Now , you know why this error is occurring. Let’s fix it!
You have to explicitly check each line of code as if there is any wrong indentation. Make sure that either all tabs are used or spaces. But there is a little problem, How we can judge if we have used space or a Tab. There is one way you can achieve this. Open up your code editor, I am using notepad++ for this.
On the top menu, you will find “view”. By clicking on view A dropdown menu will appear having an option “show symbol” where you can choose either to display tabs and spaces in your code. Once you checked the option.
You will see dots or arrows in your code. The arrow displays tabs and dots indicate spaces. This is how you can check your code and see the root of the error.
Quick Guide = view > show symbol > Show White Space and Tab
Indentation Setting in Sublime Text 3:
You can easily set indentation setting in sublime by following the steps below:
After setting up all this you are good to go and Next time you see this error 👇👇
Don’t worry just follow the given steps.
Click here to know more about Data Science Course in Hyderabad
Indentation is a little special kind of error, unlike other errors, most of the time it is caused due to tabs and spaces, not because of a syntax mistake.
Address:
360DigiTMG - Data Analytics, Data Science Course Training Hyderabad
2-56/2/19, 3rd floor,, Vijaya towers, near Meridian school,, Ayyappa Society Rd, Madhapur,, Hyderabad, Telangana 500081
Phone: 099899 94319
Agra, Ahmedabad, Amritsar, Anand, Anantapur, Bangalore, Bhopal, Bhubaneswar, Chengalpattu, Chennai, Cochin, Dehradun, Malaysia, Dombivli, Durgapur, Ernakulam, Erode, Gandhinagar, Ghaziabad, Gorakhpur, Gwalior, Hebbal, Hyderabad, Jabalpur, Jalandhar, Jammu, Jamshedpur, Jodhpur, Khammam, Kolhapur, Kothrud, Ludhiana, Madurai, Meerut, Mohali, Moradabad, Noida, Pimpri, Pondicherry, Pune, Rajkot, Ranchi, Rohtak, Roorkee, Rourkela, Shimla, Shimoga, Siliguri, Srinagar, Thane, Thiruvananthapuram, Tiruchchirappalli, Trichur, Udaipur, Yelahanka, Andhra Pradesh, Anna Nagar, Bhilai, Borivali, Calicut, Chandigarh, Chromepet, Coimbatore, Dilsukhnagar, ECIL, Faridabad, Greater Warangal, Guduvanchery, Guntur, Gurgaon, Guwahati, Hoodi, Indore, Jaipur, Kalaburagi, Kanpur, Kharadi, Kochi, Kolkata, Kompally, Lucknow, Mangalore, Mumbai, Mysore, Nagpur, Nashik, Navi Mumbai, Patna, Porur, Raipur, Salem, Surat, Thoraipakkam, Trichy, Uppal, Vadodara, Varanasi, Vijayawada, Visakhapatnam, Tirunelveli, Aurangabad
ECIL, Jaipur, Pune, Gurgaon, Salem, Surat, Agra, Ahmedabad, Amritsar, Anand, Anantapur, Andhra Pradesh, Anna Nagar, Aurangabad, Bhilai, Bhopal, Bhubaneswar, Borivali, Calicut, Cochin, Chengalpattu , Dehradun, Dombivli, Durgapur, Ernakulam, Erode, Gandhinagar, Ghaziabad, Gorakhpur, Guduvanchery, Gwalior, Hebbal, Hoodi , Indore, Jabalpur, Jaipur, Jalandhar, Jammu, Jamshedpur, Jodhpur, Kanpur, Khammam, Kochi, Kolhapur, Kolkata, Kothrud, Ludhiana, Madurai, Mangalore, Meerut, Mohali, Moradabad, Pimpri, Pondicherry, Porur, Rajkot, Ranchi, Rohtak, Roorkee, Rourkela, Shimla, Shimoga, Siliguri, Srinagar, Thoraipakkam , Tiruchirappalli, Tirunelveli, Trichur, Trichy, Udaipur, Vijayawada, Vizag, Warangal, Chennai, Coimbatore, Delhi, Dilsukhnagar, Hyderabad, Kalyan, Nagpur, Noida, Thane, Thiruvananthapuram, Uppal, Kompally, Bangalore, Chandigarh, Chromepet, Faridabad, Guntur, Guwahati, Kharadi, Lucknow, Mumbai, Mysore, Nashik, Navi Mumbai, Patna, Pune, Raipur, Vadodara, Varanasi, Yelahanka