“inconsistent use of tabs and spaces in indentation”


“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):
print("")
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):
print("")
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):
print("")
print(self.val)
tal = random.randint(0,100)
if tal < 20:
print("åkturen gick åt skogen, bättre lycka nästa gång")
elif tal >= 20:
print("jabbadabbbadoooooooo")
return 1

#går i spökhuset
def aka1(self):
print("")
print(self.val)
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

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

while 1:
vald_attr = input("Vad vill du göra?n1. frittfalln2. bergodalbanan3. spökhusetn4. Avslutan")
if vald_attr == "1":
val = Nojesfalt(attraktioner[0])
if val.langdgrans() == 1:
val.aka()
elif vald_attr == "2":
val = Nojesfalt(attraktioner[1])
val.aka()
elif vald_attr == "3":
val = Nojesfalt(attraktioner[2])
if val.aldersgrans() == 1:
val.aka1()
elif vald_attr == "4":
break





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.
– Jeff Mercado
Apr 16 '11 at 8:49






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.
– Lennart Regebro
Apr 16 '11 at 10:10





13 Answers
13



Don't use tabs.



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.





@detly: Ho hum, I somehow conflated two points when I wrote that, Fixed now, thanks! :-)
– Lennart Regebro
Apr 16 '11 at 12:07





Is there any reasoning behind these dogmas?
– RocketR
Jul 25 '11 at 22:24





@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.
– Lennart Regebro
Jul 26 '11 at 9:28






@RocketR Python has Pep8, a document which lists "good python style" which explicitly states that 4 spaces is the accepted form on indentation.
– jozefg
Sep 14 '13 at 12:13





Link to relevant PEP 8 section on "Tabs vs Spaces" spoiler: the first line is "Spaces are the preferred indentation method."
– Tadhg McDonald-Jensen
May 12 '16 at 19:01



With the IDLE editor you can use this:



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.



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





On the command line you can also use expand or unexpand
– JoshAdel
Apr 16 '11 at 13:49


expand


unexpand



I recently had the same problem and found out that I just needed to convert the .py file's charset to UTF-8 as that's the set Python 3 uses.



BTW, I used 4-space tabs all the time, so the problem wasn't caused by them.



I had this problem. Solution: your 'tab' before the problem line is spaces, 4, 6, or 8 spaces. You should erase them and insert TAB. 't'. That's all.





It is not recommended using TAB characters at all when coding with Python - read PEP008 (python.org/dev/peps/pep-0008)
– Tony Suffolk 66
Oct 25 '15 at 7:20



I got the same errors but could not figure out what I was doing wrong.



So I fixed it by running auto-indent on my code and allowing the machine to fix my fault.



If anyone is wondering how I did that.
Simple.
Go in vim.
Type in G=gg.



This will automatically fix everything. Good luck :)



Your problem is due to your editor limitations/configuration. Some editors provide you of tools to help with the problem by:



Converting tabs into spaces



For example, if you are using Stani's Python editor you can configure it to do it on saving.



Converting spaces into tabs



If you are using ActiveState Komodo you have a tool to 'tabify' your code. As others already pointed, this is not a good idea.



Eclipse's Pydev provides functions "Convert tabs to space-tabs" and "Convert space-tabs to tabs".



I had the same error. I had to add several code lines to an existing *.py file. In Notepad++ it did not work.



After adding the code lines and saving, I got the same error. When I opened the same file in PyCharm and added the lines, the error disappeared.



What I did when the same error popped up: Select everything (Str + A) and press Shift + Tab. So nothing was indented anymore. Now go back to the lines you want to have indented, and put it back how you want it.



It worked for me...



Sometimes, tab does mess up while indenting. One way is to obviously use the tab and backspace to correctly indent the code.


tab


tab


backspace



Another way is to use space 4 times (depending on how much you want to indent).


space



A weird way that worked for me when nothing else worked, whichever line I getting the error, I backspaced that line to the previous line and then pressed enter. It automatically indented the line to correct position and I was not getting any error after that.


backspaced


enter



Hopefully, this should help.



I use Notepad++ and got this error.



In Notepad++ you will see that both the tab and the four spaces are the same, but when you copy your code to Python IDLE you would see the difference and the line with a tab would have more space before it than the others.



To solve the problem, I just deleted the tab before the line then added four spaces.



Highligt the indented bits, and then click tabs. They will then become consistent :)





This conflicts with PEP 8, the canonical Python style guide, which recommends 4 spaces. Advising people to use tabs is more inconsistent.
– Aurora0001
Dec 2 '16 at 17:26





not to mention that if collaborators on this code that are using PEP8 will encounter the same error
– Thomas Matthew
Jan 28 '17 at 5:59




Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).


Would you like to answer one of these unanswered questions instead?

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages