This issue is really strange, I can't figure out what's causing it. I'm using Python in VSCode and there's something weird with for loop indentation. For example, this works fine:
for i in [0,1]: print(f"First line {i}")
But if I add another line, it fails:
for i in [0,1]: print(f"First line {i}") print(f"Second line {i}")
Note how the indentation in the terminal doesn't match. In fact every time I run a for loop the indentation in the terminal seems to increment until it's far to the right! However this
does work:
for i in [0,1]: print(f"First line {i}") print(f"Second line {i}")
Which makes no sense. Is this some kind of issue with VSCode or Python?
What, exactly, do you wish to achieve? Please elaborate.
Why indent the second print() statement in the same for loop? The second indentation tells the Python interpreter, "I am now leaving this for loop"
for i in range(0,4): print(f"First line {i}") for j in range(2, 4): print(f"Second line {i+j}")Output:
First line 0 Second line 2 Second line 3 First line 1 Second line 3 Second line 4 First line 2 Second line 4 Second line 5 First line 3 Second line 5 Second line 6
Or maybe you want this:
for i in range(0,4): print(f"First line {i}") print(f"Second line {i}") Make sure your editor inserts 4 space characters when you hit the tab key (instead of a tab character).
The same issue was reported here:
https://github.com/python/cpython/issues/121400 It was not resolved, and nobody could reproduce the issue.
I was able to reproduce by copying from notepad and pasting into the terminal one line at a time.
Output:
>>> for i in range(5): ... print(i) ... print(i**i) ... File "<python-input-0>", line 3 print(i**i) IndentationError: unexpected indent >>>
Is that what you did? Copy and paste lines one at a time into the interpreter? Why are you entering code in the interactive interpreter instead of running the file that contains the code?
The code was just for testing purposes, it doesn't do anything. It wasn't pasted into the interpreter, it was whenever I ran the code in a block. And yeah VSCode inserts 4 spaces no matter what so that wasn't it (the problem still occurred no matter what kind of spacing I used anyway). I was finally able to fix the issue by downgrading Python to 3.12.10 - apparently it's a known bug if you're using Python on Windows?
P.S. This was on VSCode 1.103.2 and I was initially using Python 3.13.7 if that helps anyone.
the >>> prompt means you are running the interactive python interpreter.
I don't think this has anything to do with VSCode. I started an interpreter in the windows cmd shell outside VSCode and I get the same behavior. However, I cannot get the auto-indenting to be off by typing into the interpreter, only pasting. It appears that the interpreter is being helpful and auto indenting the code. The first line after the for loop is indented 1 level deeper. Lines after that are indented the same amount as the previous. This becomes a problem if you paste code that is already indented.
source:
for i in range(5): print(i) print(i**i)
Pasting into interpereter
Output:
>>> for i in range(5): ... print(i) # interpreter indents 1 level following for statement. Indent from source adds a second indent. ... print(i**i) # interperter indents to level of previous statement. Indent from source adds a third indent.
I can reproduce the results without cutting and pasting if I enter a tab at the start of the print lines, but why would I do that since the interpreter is doing the indent for me?
Yeah as posted at the top of the thread, if I removed all indentation it worked (since the spaces the interpreter was adding actually worked in that instance). It's very strange, who would want that behaviour? Python that only works if you don't intent it properly? I assume it's some kind of bug, since downgrading Python to 3.12 fixed it.
Anyway everything seems to be working now, thanks for the help guys.
Quote:Yeah as posted at the top of the thread
Nowhere did you say how you were entering code or how you were running this code.
Quote:who would want that behaviour
Most people, I think. The python interpreter automatically tabs (correctly) as you type code into the interpreter. Tabs are only a problem when you copy and paste into the interactive interpreter. I don't think that's very common. I'm backed up by how hard it was to find the issue reported on coding forums.
I don't understand why you paste code into the interpreter. If you want to run code that's already in a file, run the file. If you want to single-step, run in the debugger. There are better tools than copy/paste.
Pasting code into the REPL is an expected use case. The 3.13 release was supposed to have several changes to improve that experience. With my particular terminals and OS, I couldn't replicate the problem as described.
Might be a Windows python thing. Does it for python 3.13.5 on my windows 11 computer. I remember seeing the problem with 3.11 and maybe as far back as 3.8 one windows 10.