user18214
user18214

Reputation: 121

why won't visual studio code run my code?

For example if I run in vs code the following:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

what it shows in the terminal is:

for x in fruits:
...     print(x)
...

So the next thing I tried to do is run it line by line, but when I only run

fruits = ["apple", "banana", "cherry"]

It shows in the terminal

fruits = ["apple", "banana", "cherry"]
         ^
SyntaxError: invalid syntax

I don't know what the reason is, and I know nothing about programming, but this is supposed to work I think. I use anaconda navigator, and for some reason jupyter notebook works flawless, but vs code is constantly throwing errors at whatever I do. And sometimes it suddenly works and actually runs a command. Anyone got an idea?

In jupyter notebook the output is:

apple
banana
cherry

Upvotes: 1

Views: 589

Answers (3)

eirikg
eirikg

Reputation: 47

Sometimes I have the same problem. Just run the code with the Command Prompt/Power Shell it is better anyways. Navigate with "cd examplelocation" to your file Folder and then type in the programs name.

Example:

C:\Users\eirik>

cd desktop

C:\Users\eirik\Desktop>

cd python

C:\Users\eirik\Desktop\Python> (Folder were my program is stored)

example.py

Upvotes: 2

Chrispresso
Chrispresso

Reputation: 4061

You need to execute the script. You can do this through the command palette by hitting ctrl + shift + p and then typing python: Run file in terminal. That will run the specific file that you're currently looking at within the window.

Upvotes: 2

Countour-Integral
Countour-Integral

Reputation: 1154

You can seperately run it in a terminal if you just want a solution by typing

python youscriptname.py

Right now you are not running the script in vs-code, You are just opening a python shell with that code

Upvotes: 0

Related Questions