After slightly altering an exercise from LP3THW, I started to receive an indentation error when running the script for a second time in Powershell: "TabError: inconsistent use of tabs and spaces in indentation".
I set the editor to show the indents and it does show some space mixed up with tabs, even though I consistently use tabs only when I type. I tried indenting those specific lines several times and it doesn't work (they end up showing as dots, as for space, instead of arrows, as for tab).
Am I doing something wrong...?
(I'm using Python 3.8 in Windows 10)
(lines 9 to 15)
EDIT: I consulted the answer in
Convert tabs to spaces in Notepad++
in order to change the settings for Tab.
Don't know what originated the error in the first place, but the script works now
Related
I've been using vim for a few weeks, and so far I've downloaded a few plugins including airline, nerdtree, and a colorscheme. When I downloaded some files with wget, I noticed that they weren't properly aligned. Text in the code files are normally supposed to be aligned in columns, and there generally shjouldn't be issues with indentation. As an example, when I try running the command cat filename.extension, I get this:
This is how the file is supposed to look. Everything is aligned neatly into columns without any issues. However, if I try editing the file in Vim, it instead looks like this:
The text for whatever reason is not aligned properly, and so far I am not sure how to resolve it...
The file appears to be using tabulations for indentation and alignment.
The de-facto standard width of a tabulation is 8 characters.
cat respects that "standard" to the letter, without the possibility of changing the tabulation width.
Vim also respects it by default but it allows the user to change the tabulation width and you tell it to use a tabulation width of 4 characters.
The file looks different because the two programs have different tabulation settings.
Therefore, if you want the file to look the same in the two programs, use the same tabulation settings. Since they can't be changed in cat, you will have to revert Vim's to their default values.
You didn't show us your vimrc so we don't know exactly what you did and we can't really tell you how to go about it.
This is not a programming question but an inconvenience in the android-studio editor.
If you have an unwanted tab before all your lines, how can you remove them all at once? Now I have to manually go over 50 lines to remove all the tabs to make my code look clean.
If you want to add multiple tabs at once you just select all your code and press the tab button. So I'm looking for the reverse of that.
If I understood you right, you want to beautify the code itself. Fortunately, you don't have to do that manually - at all.
There's a keybinding for it, which may vary by your OS and which layout you use by default. Go to file -> settings -> Keymap and search for auto-indent. Here's what I get on Windows 10 with the default keymap:
Again, which you have may depend on OS (I'm assuming that mainly applies to Mac though) and your keymap, but you can automatically indent your code as per language standards using Ctrl+Alt+I.
Note that this mainly does indentation. If you chose to golf code and want to ungolf it, this will not work. At least it doesn't for Java.
However: This only works with code files the IDE or plugins support. This won't work for i.e. a .txt file out of the box.
If I misunderstood you and you only want to remove tabs without doing the auto-indent, there's at least two other options.
The first option is using multiple cursors. You can add an additional cursor with shift+alt+a mouse click where you want the cursor, or holding the mouse wheel and moving the cursor with the mouse wheel held down. There might be other methods as well, but those are the two I know of.
Once you have multiple cursors, delete the tabs just like normal. But be careful! Doing so might delete the entire line itself. If it does, you can do 1 tab/n units per indent level to the left, and press delete instead.
There's (AFAIK) no limit to how many cursors you can have at once, but you can theoretically do it with 50 lines at once if you want to. But general advice - don't add more cursors than you can see at once. These do run in parallel and it's easy to lose track if you're not careful, and you might end up deleting stuff you didn't want to delete.
And finally, the regex solution:
Note: Be careful with this. If you use it incorrectly, you might get unwanted results.
If you only want to do this in a limited area, highlight it first. Then CTRL+R and you'll be presented with the regular replace menu. Make sure Regex and In Selection are selected.
A base regex to go off is ^([\s]{2,4}|\t). Explanation just for reference:
^ - At the start of the line
(
\s{4} - Match 4 spaces
|\t - Or a tab character
)
Replace with nothing and click "replace all" (or just use the regular "replace" button if you want to double-check before you do anything). This will replace one occurrence of 4 spaces or a single tab character. If you use indentation that isn't based on 4, change the number.
This is only useable and useful if you've found yourself with incorrect indentation that's the same across all the relevant lines - it will not fix indentation mistakes and/or inconsistencies such as 3-space indentation when you want 4, or random indetation for the same block. Use the first or alternatively second method for that instead.
I've been reading PEP 8 but I don't understand why Python3 bothers to single out mixing tabs and spaces with this error message.
TabError: inconsistent use of tabs and spaces in indentation
I understand the importance of consistency but an error message that halts execution seems extreme, especially since mixed tabs and spaces doesn't make any difference to the compiler and most good editors support tab/space conversions. On top of that, Python3 doesn't have a problem with you ignoring other PEP conventions (for example using three space indentations instead of four), you won't even get a warning message for it.
So what's the deal, why does Python3 all of sudden treat mixing spaces and tabs like the ultimate evil?
You can perfectly well mix tabs and spaces in a python file (though PEP8 says you shouldn't).
(Stackoverflow prints tab as 4 spaces, example tabs are correct in this gist; test them out yourself!)
Here's a perfectly valid python function:
def foo():
print("one tab") # all indentation is one tab.
if True:
print("two spaces")
else:
print("four spaces")
what you can't do is mix tabs and spaces in the same block:
def foo():
print("one tab")
print("eight spaces")
This gives a TabError.
Even if these lined up in your text editor.
This is because (roughly) they may line up differently in someone else's text editor and look like a different procedure. To give a silly example:
def foo():
while True:
"we should exit immediately"
return True # suppose this was tab indented
The last line may look like it lines up with the string, and hence returns immediately, but since it's tab indented perhaps it's lined up with the while (so the while loop never exits). ???
Here lies the ambiguity, and so the parser say no.
However, PEP8 says you should just always use 4 spaces, avoiding this ambiguity.
(English is not my native language so there might be mistakes here:)
I was trying to change the visual style of Sublime Text 3, I installed the "https://github.com/kkga/spacegray" but were stopped by an error. This was not the first time and have got this message before:
Error trying to parse settings: Unexpected character, expected a comma or closing bracket in Packages\User\Preferences.sublime-settings:19:1
As you can see it highlights the colon but can't fix it either way, every setting above the style are not in use now because it only accepts (and activates the style) if I cut out everything else like this:
I tried changing characters and finding answers for this problem but have been unsuccessful until now. My settings
The problem is that you are missing a comma there in line 18, it should be:
"word wrap": false,
That's why you get the parsing error, after each option there must be either a comma, indicating that there are more config parameters remaining, or a closing bracket }.
When adding new settings, make sure that all of them are separated by commas.
I know there are some threads about this on stackoverflow but when i write ":set list" in the editor, it seems to display hidden characters but it doesnt display the hidden characters in the code we are having problems with.
Some times now we have had some invisible symbols in our code making if loops break, i dont know how the symbols get there except from that some wierd keyboard combination much have been accidentally typed in. The code itself looks correct but the invisible symbol breaks it.
I have searched online about this but all i can find seems to be the ":set list" command in vim in addition to have to change the color of the hidden characters, but while this seems to display some hidden characters it doesnt display the problematic ones. We are getting two symbols which looks like a cross and one looks like a pistol. We have also tried to add the "draw_white_space" setting in sublime text but this only seems to display, well, whitspace like it says but the result was shown on google for showing hiden characters so i gave it a try.
The only way we have been able to see where the symbols are is with the DiffMerge tool, we have not been able to see these symbols in any other editor but we have actually been able to copy the sign to its own file and grep through all the files with the -f grep option which works, but it would be easier to display the characters in vim but using a keybinding.
Does someone have any suggestions? This is causing us to use a lot more time debugging the code when the problems is an invisible symbol.
Try the following search command:
/[^ -~<09>]
(you get the <09> by pressing the tab key). Or if you want to get rid of those nasty tabs, just:
/[^ -~]
That will find and highlight any non-ASCII or control-ASCII character.
If you still have hidden characters out there, you can try this command before the search:
:set enc=latin1
That will prevent any weird Unicode character to show up in your code.