TabError inconsistent use of tabs and spaces in indentation
In Python, You can indent using tabs and spaces in Python. Both of these are considered to be whitespaces when you code. So, the whitespace or the indentation of the very first line of the program must be maintained all throughout the code. This can be 4 spaces, 1 tab or space. But you must use either a tab or a space to indent your code.
But if you mix the spaces and tabs in a program, Python gets confused. It then throws an error called “TabError inconsistent use of tabs and spaces in indentation”.
In this article, we delve into the details of this error and also look at its solution.
How to fix 'TabError: inconsistent use of tabs and spaces in indentation'?
Example:
a = int(input("Please enter an integer A: "))
b = int(input("Please enter an integer B: "))
if b > a:
print("B is greater than A")
elif a == b:
print("A and B are equal")
else:
print("A is greater than B")
Output:
TabError: inconsistent use of tabs and spaces in indentation
When the code is executed, the “TabError inconsistent use of tabs and spaces in indentation”. This occurs when the code has all the tabs and spaces mixed up.
To fix this, you have to ensure that the code has even indentation. Another way to fix this error is by selecting the entire code by pressing Ctrl + A. Then in the IDLE, go to the Format settings. Click on Untabify region.
Solution:
1. Add given below line at the beginning of code
#!/usr/bin/python -tt
2. Python IDLE
In case if you are using python IDLE, select all the code by pressing (Ctrl + A) and then go to Format >> Untabify Region
So, always check the placing of tabs and spaces in your code properly. If you are using a text editor such as Sublime Text, use the option Convert indentation to spaces to make your code free from the “TabError: inconsistent use of tabs and spaces in indentation” error.