• Register
0 votes
216 views

Problem :

I'm new to Python & 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.
6.9k points

2 Answers

0 votes

Solution:

It is possible to solve this issue by using notepad++ by replacing Tabs with 4 Spaces:

  1. Choose Search -> Find
  2. Select the Replace tab
  3. In the box named Search Mode choose Extended(\n, \r, \t, \0, \x...)
  4. In the field Find what : write \t
  5. In the field Replace with : press Space 4 times. Be sure that there is nothing else in this field.
  6. Click on the button Replace All

How to replace Tabs with Spaces

Further Readings:

https://github.com/ntasfi/PyGame-Learning-Environment/issues/11

36.1k points
edited by
0 votes

Solution:

  1. Fix your editor to use 4 spaces for indentation.

  2. Create a search and replace to replace all tabs with 4 spaces.

  3. Ensure that your editor is set to display tabs as 8 spaces.

Note: The cause for 8 spaces for tabs is so that you instantly notice at the time tabs have been inserted unconsciously - such as at the time copying and pasting from example code that exercise tabs instead of spaces.

Employing the autopep8 command below fixed it for me:

autopep8 -i my_file.py

Documentation for autopep8 linked here.

With the IDLE editor you can exercise this:

  • Menu Edit → Select All

  • Menu Format → Untabify Region

  • Considering your editor has replaced 8 spaces with a tab, enter 8 into the input box.

  • Press select, and it fixes the entire document.

In case you are employing Sublime Text for Python development, you can eliminate the error by exercising the package Anaconda. Afterwards installing Anaconda, open your file in Sublime Text, right click on the open spaces → Select Anaconda → click on autoformat. Done. Or hit Ctrl + Alt + R.

Usually, people set up indenting with space. It's more compatible across editors, resulting in several mismatches of this sort. But, you are approved to indent with tab. It's your option; but, you must be aware that the standard of 8 spaces per tab is a bit wide.

Involving your problem, most likely, your editor messed up. To alter tab to space is actually editor-dependent.

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

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

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

What I did at the time the similar error popped up: choose everything (Str + A) and hit Shift + Tab. So nothing was requistioned anymore. Currently go back to the lines you want to have requistioned, and put it back how you want it.

I currently had the similar problem and traced out that I only required to alter the .py file's charset to UTF-8 as that's the set Python 3 uses.

In Sublime Text, WHILE editing a Python file:

Sublime Text menu > Preferences > Settings - Syntax Specific :

Python.sublime-settings

{
    "tab_size": 4,
    "translate_tabs_to_spaces": true
}

At the time exercising the sublime text editor, I was able to choose the segment of my code that was giving me the inconsistent use of tabs and spaces in indentation error and choose:

view > indentation > convert indentation to spaces

16.4k points
edited by

Related questions

0 votes
1 answer 23 views
23 views
Problem: I am a newbee in python programming. I was doing my coding practice. I wrote some codes like : class MyCar: def __init__(self,rang,oil_remaining,**kwargs): self.rang=rang self.fuel_remaining=oil_remaining self.taps = 0 for key,value in kwargs.items(): setattr(self ... and spaces in indentation The code seems right to me but I dont know why am I getting this type of errors. Please help me
asked Mar 22 LizzyM 6.1k points
0 votes
1 answer 17 views
17 views
Problem: I am new to python and I have implemented the list in python and trying to access the list index but it getting me an error that is intend error by for loop don&rsquo;t know what I am doing wrong in this code. I come from the C background so ... File "main.py", line 46 break ^ IndentationError: unindent does not match any outer indentation level Need someone to help me waiting for help!!
asked Jun 29 Gavin 15.2k points
1 vote
1 answer 17 views
17 views
Problem: I tried to check my dictionares using if the dictionary is in use or not. To check that I wrote something like: def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users ... : socket.send("ONLINE " + ' ' .join(self.users.keys())) But the program is not working properly what should I do?
asked Apr 3 LizzyM 6.1k points
0 votes
1 answer 48 views
48 views
Problem : I am new to Python, I want to do the simple nearest neighbors classification but I am always facing below error while trying to execute following code. &ldquo;ValueError: Found input variables with inconsistent numbers of samples: [489, 1890]&rdquo; My code snippet as below : myneigh = KNeighborsClassifier(n_neighbors=3) myneigh.fit(X_bus, y_bus) How can I fix above error?
asked Feb 25 mphil 2.3k points
0 votes
1 answer 47 views
47 views
PROBLEM: Python programming is new to me . I wrote a code which gives me following error: python indentationerror: unindent does not match any outer indentation level The code I wrote is: import sys def Function(x): result = 1 for i in range (1,x): result = result * i * i print ("Your factorial is ",result) return result print (Function(10))
asked Mar 18 LizzyM 6.1k points