Online Tutorials & Training Materials | STechies.com
Register Login

TabError: inconsistent use of tabs and spaces in indentation

28 Jan 2020 7:50 am || 0

TabError inconsistent use of tabs and spaces in indentation

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.

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

TabError: inconsistent use of tabs and spaces in indentation-1