All Python errors
TabError in Python: tabs and spaces are mixed
TabError means some lines of a block are indented with the Tab character and others with spaces, so Python cannot tell their level. To fix it, replace every tab at the start of a line with 4 spaces.
Code with the error
for number in range(2):
print(number)
print("next")
Error message (Python 3.12, last line)
Corrected code
for number in range(2):
print(number)
print("next")
Output
0
next
1
next
Common causes
- Part of the code was pasted from a source that uses tabs.
- The Tab key inserts a tab character while other lines use spaces.
- Two people edited the file with different editor settings.
- The lines look aligned on screen because the editor draws a tab 8 or 4 spaces wide.
Practise in the course
Lesson: LoopsFrequently asked questions
How do I see where the tabs are?
Turn on “show whitespace” in your editor, or delete the indent of the line and type 4 spaces again.
Is TabError a kind of IndentationError?
Yes. It is a subtype of IndentationError, so the program does not start.