• Register
0 votes
724 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 5 3
6,930 points

Please log in or register to answer this question.

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

9 7 4
38,600 points
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

10 6 4
31,120 points

Related questions

0 votes
1 answer 33 views
33 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, 2020 LizzyM 6.1k points
0 votes
1 answer 22 views
22 views
Problem: 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 ... 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.
asked Jan 26 Mashhoodch 13k points
0 votes
1 answer 30 views
30 views
Problem: 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
asked Jan 9 Mashhoodch 13k points
0 votes
1 answer 8 views
8 views
Problem Can someone give me a hand to solve What is an indentation error in python? I need help understanding this error
asked Mar 7 neeraj 10.5k points
0 votes
1 answer 37 views
37 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, 2020 Gavin 15.3k points
0 votes
1 answer 78 views
78 views
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 ... 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(" ")
asked Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 40 views
40 views
Problem: Let me start off by saying that I am COMPLETELY new to programming. I have just recently picked up Python and it has consistently kicked me in the head with one recurring error -- "expected an indented block" Now, I know there are several other ... indentation has not given me better results. I have replaced all of my indents with 4 spaces and even rewritten the code several times.
asked Jan 10 Mashhoodch 13k points
0 votes
1 answer 22 views
22 views
Problem Hey, I have been facing this error &ldquo;Block if you End if&rdquo; despite having used End if statement. So here is the proper description: I have created a combobox in in word that is created to populate the termshorthand text field which is based on the selection from the termWritten array. I am ... Then                     termShorthand.Value = "no"                 End If End Sub vba
asked Feb 4 chris jordan 2.4k points
0 votes
1 answer 5 views
5 views
Problem: Please help me to solve it. Notepad++ convert spaces to tabs
asked Mar 22 Ifra 24.4k points
0 votes
1 answer 3 views
3 views
Problem: Please help me &hellip; How to get rid of spaces in python?
asked Apr 3 Ifra 24.4k points