You can indent code using either spaces or tabs in a Python program. If you try to use a combination of both in the same block of code, you’ll encounter the “TabError: inconsistent use of tabs and spaces in indentation” error.
In this guide, we discuss what this error means and why it is raised. We’ll walk through an example of this error so you can figure out how to solve it in your code.
TabError: inconsistent use of tabs and spaces in indentation
While the Python style guide does say spaces are the preferred method of indentation when coding in Python, you can use either spaces or tabs.
Indentation is important in Python because the language doesn’t depend on syntax like curly brackets to denote where a block of code starts and finishes. Indents tell Python what lines of code are part of what code blocks.
Consider the following program:
numbers = [8, 7, 9, 8, 7] def calculate_average_age(): average = sum(numbers) / len(numbers) print(average)
Without indentation, it is impossible to know what lines of code should be part of the calculate_average_age function and what lines of code are part of the main program.
You must stick with using either spaces or tabs. Do not mix tabs and spaces. Doing so will confuse the Python interpreter and cause the “TabError: inconsistent use of tabs and spaces in indentation” error.
An Example Scenario
We want to build a program that calculates the total value of the purchases made at a donut store. To start, let’s define a list of purchases:
purchases = [2.50, 4.90, 5.60, 2.40]
Next, we’re going to define a function that calculates the total of the “purchases” list:
def calculate_total_purchases(purchases): total = sum(purchases) return total
Our function accepts one parameter: the list of purchases which total value we want to calculate. The function returns the total value of the list we specify as a parameter.
We use the sum() method to calculate the total of the numbers in the “purchases” list.
If you copy this code snippet into your text editor, you may notice the “return total” line of code is indented using spaces whereas the “total = sum(purchases)” line of code uses tabs for indentation. This is an important distinction.
Next, call our function and print the value it returns to the console:
total_purchases = calculate_total_purchases(purchases) print(total_purchases)
Our code calls the calculate_total_purchases()
function to calculate the total value of all the purchases made at the donut store. We then print that value to the console. Let’s run our code and see what happens:
File "test1.py", line 5 return total ^ TabError: inconsistent use of tabs and spaces in indentation
Our code returns an error.
The Solution
We’ve used spaces and tabs to indent our code. In a Python program, you should stick to using either one of these two methods of indentation.
To fix our code, we’re going to change our function so that we only use spaces:
def calculate_total_purchases(purchases): total = sum(purchases) return total
Our code uses 4 spaces for indentation. Let’s run our program with our new indentation:
15.4
Our program successfully calculates the total value of the donut purchases.
In the IDLE editor, you can remove the indentation for a block of code by following these instructions:
- Select the code whose indentation you want to remove
- Click “Menu” -> “Format” -> “Untabify region”
- Insert the type of indentation you want to use
This is a convenient way of fixing the formatting in a document, assuming you are using the IDLE editor. Many other editors, like Sublime Text, have their own methods of changing the indentation in a file.
Conclusion
The Python “TabError: inconsistent use of tabs and spaces in indentation” error is raised when you try to indent code using both spaces and tabs.
You fix this error by sticking to either spaces or tabs in a program and replacing any tabs or spaces that do not use your preferred method of indentation. Now you have the knowledge you need to fix this error like a professional programmer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.