TabError: inconsistent use of tabs and spaces in indentation

Screen Link:

from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

rating_count_tot = []

for row in apps_data[1:]:
    rating = int(row[5])

max_rating= max(rating_count_tot)
min_rating = min(rating_count_tot)

rating_range = {'0 - 199': 0, '200 - 399': 0, '400 - 599': 0, '600 - 799': 0, '799+': 0}

for row in apps_data[1:]:
    ratings = int(row[5])
    if ratings <= 199:
		rating_range['0 - 199'] += 1    
	elif 199 < ratings <= 399:
		rating_range['200 - 399'] += 1 
	elif 399 < ratings <= 599:
		rating_range['400 - 599'] += 1
	elif 599 < ratings <= 799:
		rating_range['600 - 799'] += 1
	elif ratings > 799:
		rating_range['799+'] += 1

And the ouput:
File “”, line 21
rating_range[‘0 - 199’] += 1
TabError: inconsistent use of tabs and spaces in indentation

I was wondering why this error occured.

Hey, Rijal.

Short answer

The short answer is that you’re getting this error because you’re sometimes using spaces, and sometimes using tabs to produce the indentation that Python requires to work properly. And you can’t mix them:


If you replace the highlighted areas with tabs, it should fix your code.

Long answer

The long answer is divided into three sections:

  1. Executing scripts from strings
  2. Reproducing the error
  3. Understanding what’s happening

You can expand to see their content by clicking on the title of each of them.

Executing scripts from strings

This would be easier to show in person. In order to make up for this, we’ll first introduce a function that will help me better detail what is happening.

The exec function accepts strings as input and executes them as if they were Python code. Let’s see an example.

>>> code_in_a_string = "x=3\nprint(x)"

Let’s see what code_in_a_string looks like.

>>> print(code_in_a_string)

So we see it is a very simple Python script:


Let’s now use exec on code_in_a_string. What is the expected output?

>>> exec(code_in_a_string)

It executed the string code_in_a_string!

Reproducing the error

Now, to reproduce the error, let’s put the following Python script in a string.

for k in range(2):

We will do this in three different ways:

  1. We willl mix tabs and spaces to indent;
  2. We will use only spaces to indent;
  3. We will use only tabs to indent;

Before we start, you should know that the tab character is \t. Let’s do this.

>>> mixed = "for k in range(2):\n    print('Something')\n\tpass"

Notice the tab character right before the pass statement. Let’s print this.

>>> print(mixed)
for k in range(2):

Notice that the indentation isn’t leveled. We’ll revisit this later.

Let’s now create a version of the script without tabs.

>>> only_spaces = "for k in range(2):\n    print('Something')\n    pass"
>>> print(only_spaces)
for k in range(2):

And now a version with only tabs for the indentation.

>>> only_tabs = "for k in range(2):\n\tprint('Something')\n\tpass"
>>> print(only_tabs)
for k in range(2):

Let’s now use the exec function on only_tabs, only_spaces and mixed and see what happens.

>>> exec(only_tabs)

It executed just fine. Let’s see how ir fairs with only_spaces:

>>> exec(only_spaces)

Worked fine! And you can guess what happens with mixed:

>>> exec(mixed)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 3
TabError: inconsistent use of tabs and spaces in indentation

We got the same error that you’re asking about!

Understanding what's happening

Now you ask:

Yo, Bruno. That’s all fine and dandy, but mixed has that weird indentation that my code doesn’t have! It’s no wonder the code for mixed doesn’t work, the lines aren’t aligned. Checkout my code below, everything is perfectly aligned, our codes do not look the same.

To which I respond:

Are you sure? Take a look at the code you pasted in the question, friend. Take a look at the orange marks.

They’re not aligned either!

You reply:

But I copy and pasted the code from the app!

The explanation for this is that there isn’t a standard for how tabs should be displayed. In the Dataquest app, tabs take up as much room as four space characters, this is also the case in many text editors.

Here on Discourse, tabs take up the same room as eight space characters. This is why the code looks so different and also why the indentation isn’t properly aligned:

The first two lines marked with an orange dot are indented using four spaces, from then on every indentation is using one or two tabs. That’s why the indentation is placed so deep inside here on Discourse.

If you use tabs instead of spaces in the first two lines marked with an orange dot, your code will work.
Alternatively replace each tab character with four spaces.

In the second section, I mentioned that we would revisit the fact that the indentation levels when printing mixed aren’t aligned. This happens because in my terminal window, where I ran these examples, the tab character takes up as much space as eight characters when printed.

1 Like

Hi, Bruno.

Thanks a lot!!!
It works, and i just realised i mixed both of them when i wrote that code.

1 Like