"Inconsistent use of tabs and spaces indentation"

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

How to change spaces to tabs? It drives me crazy. (I am new to programming). I would be glad if I could get some general advice on my code, if I made many 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. frittfall\n2. bergodalbana\n3. spökhuset\n4. Avsluta\n") 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 
+164
python
Apr 16 2018-11-11T00:
source share
23 answers

Do not use tabs.

  • Set 4 spaces for indentation in the editor.
  • Do a search and replace to replace all tabs with 4 spaces.
  • Make sure your editor is set to display tabs as 8 spaces.

Note. The reason for the 8 spaces for tabs is that you will immediately notice when the tabs were pasted unintentionally - for example, when copying and pasting code from an example that uses tabs instead of spaces.

+214
Apr 16 '11 at 10:12
source share

With the IDLE editor you can use this:

  • Edit Menu → Select All
  • Menu Format → Untabify Region
  • Assuming your editor has replaced 8 spaces in a tab, enter 8 in the input field.
  • Press select and it will fix the whole document.
+36
Jun 06 '15 at 20:20
source share

Using the autopep8 below fixed this for me:

  autopep8 -i my_file.py 

The documentation for autopep8 here.

+23
Dec 28 '18 at 3:20
source share

As a rule, people prefer padding with space. This is more consistent between editors, resulting in fewer inconsistencies of this kind. However, you are allowed to backtrack with the tab. This is your choice; however, you should be aware that the standard of 8 spaces per tab is a little wide.

As for your problem, most likely your editor is messed up. Converting a tab to space actually depends on the editor.

For example, in Emacs, you can call the "untabify" method.

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

  sed -e 's;\t; ;' < yourFile.py > yourNedFile.py 
+11
Apr 16 '11 at 9:30
source share

If you use Sublime Text to develop Python, you can avoid the error by using the Anaconda package. After installing Anaconda, open the file in Sublime Text, right-click on open spaces -> select Anaconda -> click on autoformat. Done. Or press Ctrl + Alt + R.

+11
Jan 28 '15 at 7:12
source share

I recently had the same problem and it turned out that I just needed to convert the encoding of the .py file to UTF-8, as this uses the Python 3 suite.

By the way, I used 4-space tabs all the time, so the problem was not caused by them.

+7
Sep 14 '13 at 12:06 on
source share

What I did when the same error appeared: select all ( Str + A ) and press Shift + Tab . So there was nothing else. Now go back to the lines you want to indent and return them as you want.

It worked for me ...

+6
Jan 18 '18 at 18:05
source share

Amazing text 3

In Sublime Text, when editing a Python file :

Sublime Text Menu > Settings > Settings - Syntax :

Python.sublime setup

 { "tab_size": 4, "translate_tabs_to_spaces": true } 
+4
Aug 05 '18 at 13:27
source share

This problem can be solved with notepad++ by replacing Tab with 4 Space :

  1. Choose Search → Find ... or press Ctrl + F
  2. Select the Replace tab
  3. In the "Search Mode" field, select "Advanced" (\ n, \ r, \ t, \ 0, \ x ...)
  4. .In the Find what field: write \ t
  5. In the Replace with: field, click Space 4 times. Make sure there is nothing else in this field.
  6. Click on the Replace All button.

How to replace Tabs with Spaces

+4
Aug 20 '19 at 12:25
source share

I had this problem. Solution: your tab before the problem is spaces, 4, 6 or 8 spaces. You must delete them and paste the TAB. '\ T'. All this.

+3
Sep 19 '14 at 18:37
source share

I am using Notepad ++ and got this error.

In Notepad ++, you will see that the tab and the four spaces are the same, but when you copy the code in Python IDLE, you will see the difference, and the line with the tab will have more space in front of it than the rest.

To solve the problem, I simply removed the tab before the line and added four spaces.

+2
Aug 16 '17 at 13:26
source share

Your problem is with the limitations / configuration of your editor. Some editors provide you with tools to solve this problem:

  1. Convert tabs to spaces

    For example, if you use the Stani Python editor, you can configure it to save.

  2. Convert spaces to tabs

If you use ActiveState Komodo, you have a tool to “tabulate” your code. As others have already pointed out, this is not a good idea.

Eclipse Pydev provides the Convert Tabs to Spaces and Convert Spaces To Bookmarks features.

+1
Apr 17 2018-11-11T00:
source share

I had the same error. I had to add a few lines of code to an existing * .py file. In Notepad ++, this did not work.

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

+1
Aug 28 '17 at 9:31 on
source share

I got the same errors but couldn’t understand what I was doing wrong.

So I fixed it by executing the automatic code in my code and letting the machine fix my error.

If someone is wondering how I did it. Simply. Go to vim. Enter G = gg.

This will automatically fix everything. Good luck :)

+1
Apr 07 '18 at 21:18
source share

Sometimes a tab makes a mess during indentation. One way is to explicitly use tab and backspace to properly indent the code.

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

A strange way that worked for me when nothing worked, depending on which line I get the error, I backspaced this line to the previous line, and then hit enter . He automatically stepped back from the line for the correct position, and after that I did not get any errors.

Hope this helps.

0
May 31 '18 at 12:59
source share

I had the same problem and fix it using the following python script. Hope this helps others.

this involves using tabs and spaces for backing code. in this script, I replace each tab with four spaces.

 input_file = "source code path here" # eg source.py output_file = "out put file path here" # eg out.py with open(input_file, 'r') as source: with open(output_file, 'a+') as result: for line in source: line = line.replace('\t', ' ') result.write(line) 

if you use sublime or any other editor that gives you a tool for replacing text, you can replace all tabs with four spaces from the editor.

0
Sep 14 '18 at 12:32
source share

Another reason I don't like Python ...

If the same problem (although I used tabs sequentially, set my IDE to read 4 spaces as tabs, and in the view all indents with the code are displayed correctly and aligned).

Fortunately, this was only one line that made an error, but as soon as I deleted the tabs and entered an equivalent number of spaces, the problem was fixed.

0
Dec 03 '18 at 19:47
source share

If your editor does not recognize the tabs when performing searches and replacements (e.g. SciTE), you can paste the code into Word and search using Ctr-H and ^ t, which find tabs that can then be replaced with 4 spaces.

0
Jan 28 '19 at 18:32
source share

From here there was a duplicate of this question, but I thought that I could offer an idea of ​​modern editors and the huge number of functions that they offer. In Python code, everything that needs to be placed in a .py file must be either entirely using the tab key or spaces. The convention is to use four spaces to indent. Most editors have the ability to visually show in the editor whether the code is indented from spaces or tabs, which helps a lot when debugging. For example, with an atom, going to settings and then the editor, you can see the following two options:

atom editor to show tabs and spaces

Then, if your code uses spaces, you will see small dots where your code is indented:

enter image description here

And if you indent using tabs, you will see something like this:

enter image description here

Now, if you notice, you can see that when using the tabs on the left there are more errors / warnings, this is due to what pep8 is called, the pep8 documentation , which basically is a single style guide for python, so that’s all developers basically write code the same standard and appearance that helps when trying to understand other people's code, it is in pep8, which prefers to use spaces for indentation rather than tabs. And we see that the editor shows that there is a warning regarding warning code W191 ,

I hope that all of the above will help you understand the nature of the problem you are facing and how to prevent it in the future.

0
Feb 05 '19 at 16:27
source share

Use Idle for python or Idle3 for python3.

0
Mar 25 '19 at 15:00
source share

Your code must be consistent. Either use the entire spaces (4 spaces are recommended), or use the entire tabs. This has nothing to do with IDEs or text editors.

0
May 02 '19 at 5:39
source share

Solving this problem using the Vim editor

  1. Open terminal ( Ctrl + Alt + T ).
  2. Change to the directory where the file is located ( cd <path_to_your_directory> ). Example: cd /home/vineeshvs/work .
  3. Open the file in Vim ( vim <file_name> ). Example: vim myfile.txt .
  4. [Optional step] Turn on search keyword highlighting in Vim ( ESC :set hlsearch )
  5. Go to the line where you have this problem ( ESC :<line_number> ). Example:: :53 in the Vim editor after pressing the ESC button once.
  6. Replace the tab using the required number of spaces in Vim ( :.,$s/\t/<give_as_many_spaces_as_you_want_to_replace_tab>/gc ). Example: Tab will be replaced with four spaces with the following command:: :.,$s/\t/ /gc after pressing the ESC button once). This process is interactive. You can give y replace the tab with spaces and n skip the specific replacement. Press ESC when done with the necessary replacements.
0
Aug 24 '19 at 6:45
source share

Highlight the indented bits and click the tabs. Then they will become consistent :)

-2
Dec 02 '16 at 16:59
source share



All Articles