"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":
7/3/2018 4:03:44 AM

Using the autopep8 command below fixed it for me:

 autopep8 -i

Documentation for autopep8 linked here.


With the IDLE editor you can use this:

  • Menu EditSelect All
  • Menu FormatUntabify Region
  • Assuming your editor has replaced 8 spaces with a tab, enter 8 into the input box.
  • Hit select, and it fixes the entire document.

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.


When using the sublime text editor, I was able to select the segment of my code that was giving me the inconsistent use of tabs and spaces in indentation error and select:

view > indentation > convert indentation to spaces

which resolved the issue for me.


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;       ;' < >

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]