"inconsistent use of tabs and spaces in indentation"

I'm trying to create an application in Python 3.2 and I use tabs all the time for indentation, but even the editor changes some of them into spaces and then print out "inconsistent use of tabs and spaces in indentation" when I try to run the program.

How can I change the spaces into tabs? It's driving me crazy. (I'm a beginner in programming). I would be glad if I could get some overall tips on my code, if I have done a lot of mistakes I would be happy to hear.

import random

attraktioner = ["frittfall","bergodalbana","spökhuset"]

class Nojesfalt:
    def __init__(self, attraktion):
        self.val = attraktion
        self.langd = 0
        self.alder = 0

#längdgräns för fritt fall
    def langdgrans(self):
        self.langd = int(input("Hur lång är du i cm? "))
        if self.langd < 140:
            print("tyvärr, du är för kort, prova något annat")
            return 0
        elif self.langd >= 140:
            print("håll dig hatten, nu åker vi!")
            print(" ")
            return 1

#åldersgräns för spökhuset
    def aldersgrans(self):
        self.alder = int(input("Hur gammal är du? "))
        if self.alder < 10:
            print("tyvärr, du är för ung, prova något annat")
            return 0
        elif self.alder >= 10:
            print("Gå in om du törs!")
            print(" ")
            return 1

#åker attraktion frittfall lr bergodalbana
        def aka(self):
        tal = random.randint(0,100)
        if tal < 20:
            print("åkturen gick åt skogen, bättre lycka nästa gång")
        elif tal >= 20:
            return 1

#går i spökhuset
        def aka1(self):
        tal = random.randint(0,100)
        if tal < 20:
            print("du är omringad av spöken och kan inte fortsätta")            return 0
        elif tal >= 20:
            print("Buhuuuuuu, buuuhuuuu")
            return 1

print("Välkommen till nöjesfältet, vad vill du göra?")
print(" ")

while 1:
    vald_attr = input("Vad vill du göra?\n1. frittfall\n2. bergodalbana\n3. spökhuset\n4. Avsluta\n")
    if vald_attr == "1":
        val = Nojesfalt(attraktioner[0])
        if val.langdgrans() == 1:
    elif vald_attr == "2":
        val = Nojesfalt(attraktioner[1])
    elif vald_attr == "3":
        val = Nojesfalt(attraktioner[2])
        if val.aldersgrans() == 1:
    elif vald_attr == "4":

Don't use tabs.

  1. Set your editor to use 4 spaces for indentation.
  2. Make a search and replace to replace all tabs with 4 spaces.
  3. Make sure your editor is set to display tabs as 8 spaces.

Note: The reason for 8 spaces for tabs is so that you immediately notice when tabs have been inserted unintentionally - such as when copying and pasting from example code that uses tabs instead of spaces.

Using the autopep8 command below fixed it for me:

 autopep8 -i my_file.py

Documentation for autopep8 linked here.

Generally, people prefer indenting with space. It's more consistent across editors, resulting in fewer mismatches of this sort. However, you are allowed to indent with tab. It's your choice; however, you should be aware that the standard of 8 spaces per tab is a bit wide.

Concerning your issue, most probably, your editor messed up. To convert tab to space is really editor-dependent.

On Emacs, for example, you can call the method 'untabify'.

On command line, you can use a sed line (adapt the number of spaces to whatever pleases you):

   sed -e 's;\t;       ;' < yourFile.py > yourNedFile.py

If you are using Sublime Text for Python development, you can avoid the error by using the package Anaconda. After installing Anaconda, open your file in Sublime Text, right click on the open spaces → choose Anaconda → click on autoformat. Done. Or press Ctrl + Alt + R.

  • That's an issue with your code editor, not really with python. You should change your question to reflect that and mention what editor you are using.
  • How you change this depends on your editor. I'm not sure this is even on-topic here, it might be a question for superuser.
  • Change your editor to show visible whitespace at the start of the line. For example, I have configured my editor to show transparent tabs and visible spaces because I use tabs to indent code. If I see any noise at the start of the line, I know that there're one or more extra spaces. If you insist on doing it the incorrect way, you can configure your editor to hide spaces and show tabs. (Yeah, I know about PEP-8 but I don't agree with that.)
  • Is there any reasoning behind these dogmas?
  • @Lennart Regebro, I mean using spaces at all. I do use them but unconsciously, tabs seem better to me. By the way, in Ruby programming everyone uses 2 spaces, so it's not a single rule for all.
  • @RocketR: The answer is for Python. If you use tabs only, you get an 8-space indentation, unless you expand tabs to something else than 8 spaces, in which case it will look bad on other editors. If you mix tabs and spaces, it may break (see question) or look broken if you have other than 8-space expansion of tabs. In summary: Using tabs for indentation is incredibly bad. Never do that ever (except for languages/file formats that require it). The end.
  • @RocketR Python has Pep8, a document which lists "good python style" which explicitly states that 4 spaces is the accepted form on indentation.
  • Link to relevant PEP 8 section on "Tabs vs Spaces" spoiler: the first line is "Spaces are the preferred indentation method."
  • This fixed my problem. Thanks.
  • fixed like a charm
  • dude +1 for you
  • It is not recommended using TAB characters at all when coding with Python - read PEP008 (python.org/dev/peps/pep-0008)
  • A little elaboration on how/why this worked for you would have been helpful.
  • The problem is because of tabs instead of spaces in the region. Idle can untabify the whole region and fixes the problem (Just choose Untabify from Menu Format).