Python Forum
I am getting an IndentError on my python code in VS Code and i dont know why
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am getting an IndentError on my python code in VS Code and i dont know why
#1
Im recently learning python and im using a for function but every time that i tried to execute the code i receive the indent error. I review the youtube videos about this problem and tried every solution that i could find on the net , even reintalled my vs code ide. Now i know the code function corret because i tried it in www.online-python.com and there was no problem.

here is my pthyton code:
carros = ["mercedes", "toyota", "mazda", "bmw", "nissan", "hyundai", "byd"]

for carro in carros:
    print(f"Me encanta tu {carro.title()}, es un gran carro")
    print(f"Además me encanta el color de tu {carro.title()}.\n")
my output in vs code:
Output:
PS C:\Users\jose5\OneDrive\Documentos\Cursos_Academicos\PyThon\Crash_course> & C:/Users/jose5/AppData/Local/Programs/Python/Python313/python.exe Python 3.13.1 (tags/v3.13.1:0671451, Dec 3 2024, 19:06:28) [MSC v.1942 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> carros = ["mercedes", "toyota", "mazda", "bmw", "nissan", "hyundai", "byd"] >>> for carro in carros: ... print(f"Me encanta tu {carro.title()}, es un gran carro") ... print(f"Además me encanta el color de tu {carro.title()}.\n") ... File "<python-input-1>", line 3 print(f"Además me encanta el color de tu {carro.title()}.\n") IndentationError: unexpected indent >>> carros = ["mercedes", "toyota", "mazda", "bmw", "nissan", "hyundai", "byd"] >>> for carro in carros: ... print(f"Me encanta tu {carro.title()}, es un gran carro") ... Me encanta tu Mercedes, es un gran carro Me encanta tu Toyota, es un gran carro Me encanta tu Mazda, es un gran carro Me encanta tu Bmw, es un gran carro Me encanta tu Nissan, es un gran carro Me encanta tu Hyundai, es un gran carro Me encanta tu Byd, es un gran carro >>> for carro in carros: ... print(f"Me encanta tu {carro.title()}, es un gran carro") ... print(f"Además me encanta el color de tu {carro.title()}.\n") ... File "<python-input-4>", line 3 print(f"Además me encanta el color de tu {carro.title()}.\n") IndentationError: unexpected indent >>> KeyboardInterrupt >>> & C:/Users/jose5/AppData/Local/Programs/Python/Python313/python.exe c\>>> KeyboardInterrupt >>> & C:/Users/jose5/AppData/Local/Programs/Python/Python313/python.exe c\:/Users/jose5/OneDrive/Documentos/Cursos_Academicos/PyThon/Crash_course/f\or_practice.py File "<python-input-6>", line 1 & C:/Users/jose5/AppData/Local/Programs/Python/Python313/python.exe c:/Users/jose5/OneDrive/Documentos/Cursos_Academicos/PyThon/Crash_course/for_practice.py ^ SyntaxError: invalid syntax
here is my vs code (there isnt showing any error about indenting or linting)

i think there is a parameter in the config section about linting or indenting that is causing a conflict but i donk know where to start searching. thanks

Attached Files

Thumbnail(s)
       
Reply
#2
The code works fine for me, but that is not surprising. Posting code to the forum cleans up problems like mixing tabs and spaces for indentation. You can see in the error message that you have an indentation error.
Error:
>>> for carro in carros: ... print(f"Me encanta tu {carro.title()}, es un gran carro") ... print(f"Además me encanta el color de tu {carro.title()}.\n")
The two print statements don't line up. My guess is one line is using spaces (the first) and the other used a tab.

Python is very strict about indenting. In python, indenting is how you mark code blocks. Other languages use special markers (C++ uses {}) to mark code blocks. This is one of the common problems you'll encounter cutting and pasting code from the web. Another one I encounter a lot is using non-ascii characters in place of ascii characters. Like using starting or ending double quotes instead of plain double quotes.
Reply
#3
i already tried that. i use the tab key en each line of code but i get the same error.
Reply
#4
also whe i use a break point and the python debug file option from vs code the code function normally. i think there is a user setting causing conflict but i dont know what is it
Reply
#5
Why are you typing this code in the interactive interpreter instead of typing it into a file and running the file?

I just noticed that each time you run the code the indent gets bigger.
Output:
>>> carros = ["mercedes", "toyota", "mazda", "bmw", "nissan", "hyundai", "byd"] >>> for carro in carros: ... print(f"Me encanta tu {carro.title()}, es un gran carro") ... print(f"Además me encanta el color de tu {carro.title()}.\n") >>> carros = ["mercedes", "toyota", "mazda", "bmw", "nissan", "hyundai", "byd"] >>> for carro in carros: ... print(f"Me encanta tu {carro.title()}, es un gran carro") ... >>> for carro in carros: ... print(f"Me encanta tu {carro.title()}, es un gran carro") ... print(f"Además me encanta el color de tu {carro.title()}.\n")
How are you entering the code? Do you cut and paste or type it in? If cut and paste do you do all the lines at once or line at a time?

What shell are you using? If you run the code outside vscode does it work (start shell, run python, paste code)?

I found this that looks like it is somewhat related to what you are seeing.

https://github.com/Microsoft/PTVS/issues/1054

If you restart VSCode does the problem go away? Does this "indent walking" behavior occur if you start your interractive python shell outside VSCode?
Reply
#6
thanks to "deanhystad" i think i understand the issue. I came from a java platform "Eclipse" there you usually type your code and then run it in the ide to check if it was working ok. but in Visual Studio Code when you use Python you cant run the code in the ide, you use the section in the bottom and run in a terminal all the file as in c: python file_name.py. when i tried that the python file run ok without problem but the what purpose has the button at the top of the file that says "Run Python File" if you cant run the file in the ide.
Reply
#7
I have no problem running the file in the IDE using the run button (play button). Please describe what steps you take to run the file and what happens. There are also many YouTube videos showing how to use VSCode for python programming
Reply
#8
im learning python right now using the book "Python Crash Course 3ed" when i run code that its type in one line each time there is no problem.. But there is a problem with multilines code . for example the "for" function when you need to perform multyple operations you usually divide the code in multiple lines but in my case the IDE "vs code" say there is an indention error each time. there is no indentention error because i double check changing tab space by 4 spaces. About the steps i do to run the code in youtube videos show that when you are performing simple programs (im not into classes, inheritance, init function yet!!!) you just press the run button (play icon) ant the instructions execute normally but in my case that is not hapening when i use multiline instrucction like for, while etc. but when i run the code throught the terminal it compile and execute ok. Im certain that there is a problem with my config setttings but im not sure what to change.
Reply
#9
also i dont do "copy paste" code from internet or books because i think that you learn more typping and understanding what are you doing
Reply
#10
i just discover that if i execute the code pressing ctrl+F5 the code execute normaly. my only probles is that start button at the top
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python code not running Azdaghost 1 156 Apr-22-2025, 08:44 PM
Last Post: deanhystad
  Mathematica code vs Python MiloChocolatito 0 492 Mar-31-2025, 06:32 AM
Last Post: MiloChocolatito
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 284 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  How do i run python code? smattiko83 5 698 Mar-06-2025, 09:20 PM
Last Post: ArchieLinux
  I'm new to Python - can someone help with this code as it is not working? lminc123 1 543 Feb-13-2025, 06:13 PM
Last Post: lminc123
  I cannot create a virtual environment on visual studio code using python Willem_Aucamp316 2 3,431 Nov-27-2024, 02:20 PM
Last Post: menator01
  CLI to python code azxo1 11 2,224 Oct-21-2024, 08:32 AM
Last Post: azxo1
  Algorithm for extracting comments from Python source code Pavel1982 7 2,937 Aug-28-2024, 02:50 AM
Last Post: timothyferriss
  Coding help required in Python using VS Code KakashiSenpai 3 1,133 Jul-04-2024, 12:32 PM
Last Post: jefsummers
  Merge Python code with Micro Python code? adzy 2 970 Jul-03-2024, 11:41 AM
Last Post: kkinder

Forum Jump:

User Panel Messages

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