Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append inside for?
#1
This works:

opts1 = []
for idx, choice in enumerate(choices):
	 opts1.append (str(idx))
And this works:

menu = ""
for idx, choice in enumerate(choices):
	 menu +=  str(idx).ljust(6) + choice  + '\n' 
But this gives error

menu = ""
opts1 = []
for idx, choice in enumerate(choices):
	 menu +=  str(idx).ljust(6) + choice  + '\n' 
	 opts1.append (str(idx))
Inconsistent use of tabs and spaces in indentationPylance

What am i doing wrong?
Reply
#2
There is no error in the posted code. The error message indicates that sometimes you indent using tabs and other times using spaces. I think this gets cleaned up by the python tags.

This code runs fine for me:
choices = list("ABCD")
menu = ""
opts1 = []
for idx, choice in enumerate(choices):
    menu += str(idx).ljust(6) + choice + "\n"
    opts1.append(str(idx))
But this code that looks identical generates the error you are seeing.
choices = list("ABCD")
menu = ""
opts1 = []
for idx, choice in enumerate(choices):
    menu += str(idx).ljust(6) + choice + "\n"
	opts1.append(str(idx))  # Starts with a tab instead of 4 spaces.
Error:
opts1.append(str(idx)) TabError: inconsistent use of tabs and spaces in indentation
Next time you post be sure to include the entire error message.
johnywhy likes this post
Reply
#3
(Jan-13-2024, 07:06 PM)deanhystad Wrote: There is no error in the posted code. The error message indicates that sometimes you indent using tabs and other times using spaces. I think this gets cleaned up by the python tags.
Oops! I have vs code set to tabs. I guess some spaces got in there by mistake.

Quote:TabError: inconsistent use of tabs and spaces in indentation[/error]
Next time you post be sure to include the entire error message.
Thumbs Up i thought i did.
Reply
#4
Hi,

(Jan-13-2024, 07:14 PM)johnywhy Wrote: Oops! I have vs code set to tabs. I guess some spaces got in there by mistake.
That's not the recommended best / common practice... Although Python accepts tab or any number of spaces for indention - as long as idention is consistant - using 4 spaces per level of indention is what is recommended to use. See also the PEP8 on this.

Proper editor can also be set-up to replace a tab with 4 blank spaces when programming Python code.

Regards, noisefloor
johnywhy likes this post
Reply
#5
(Jan-13-2024, 07:28 PM)noisefloor Wrote: using 4 spaces per level of indention is what is recommended to use. See also the PEP8 on this.

I read the PEP8, but it doesn't explain why 4 spaces are preferred to tab.
https://legacy.python.org/dev/peps/pep-0...-or-spaces

I find tabs easier to count. If there are 4 tabs, i've got 4 perfect indents. If there are 16 spaces, that's more to count, and harder to interpret visually. 15 spaces? 17 spaces? Still looks correct!
Reply
#6
Every editor I use has the option to tab using spaces.
johnywhy likes this post
Reply
#7
(Jan-14-2024, 01:19 AM)deanhystad Wrote: Every editor I use has the option to tab using spaces.

I didn't say my editor doesn't have that option. I just prefer tabs.
Reply
#8
Why? Do you like not knowing how your code will appear when viewed in a different editor? Or maybe you like not being able to cut and paste python code snippets you find online, all of which use 4 space tabbing? Or is your plan to never write modules for anyone else to use?

Indenting in Python is not just for looks. Indentation is syntax for code blocking. Unlike languages that use bracketing characters, Python is intolerant of changing the code blocking "style". If everyone uses the same style, this works fine. If you want to be different, Python will bite you again and again. Get used to that "inconsistent use" error. You will be seeing it a lot. I speak from experience.
Reply
#9
i understand the purpose of whitespace in python.

i understand that i'll have to convert spaces to tabs if i copy someone else's code of the internet. I'm ok with that.

I don't see an issue with how my code looks in other editors. Every ascii-capable device can display tabs.

I prefer tabs because it's visually more reliable.

With spaces, could be 3, or 4, or 5... hard to tell. Easier to make a mistake.

Since a tab is a big chunk of whitespace, i'm less likely to confuse 1 tab from 2 tabs.

Also one tab is less typing than 4 spaces.

Personal preference. Python supports both. vscode supports both. I'm allowed to have my personal preference.
Reply
#10
(Jan-15-2024, 12:13 AM)johnywhy Wrote: Python supports both. vscode supports both. I'm allowed to have my personal preference.
In your Python journey, you will probably change some of your preferences to ease communication with other pythonistas. I strongly support formatting Python code with the black formatter, which frees your mind from code formatting. By default, Black uses spaces. Also the historic reindent script by Tim Peters can replace tabs by spaces automatically.
johnywhy likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,611 Apr-22-2020, 01:01 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020