2

I'm new to programming language and started learning python. While practicing writing code in VS code editor, I'm getting invalid syntax error for the following code:

sidekick = "WonderBoy"
print("You ruined the Grill Master's barbeque!")
print("The crowd is chanting your name!")
print(sidekick * 20)

But When I type the same code in python own shell editor, above code executes well. Could anyone tell me what's wrong with the code.

Code Editor: Visual Studio Code

Error occurring instance: Right click and select "Run python file in terminal"

Error:

>SyntaxError: invalid syntax
>>> & C:/Users/abhis/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/abhis/Desktop/Ex_Files_Programming_Foundations_Fundamentals/Exercise Files/Chap02/text1.py"
  File "<stdin>", line 1
    & C:/Users/abhis/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/abhis/Desktop/Ex_Files_Programming_Foundations_Fundamentals/Exercise Files/Chap02/text1.py"
    ^
SyntaxError: invalid syntax
kaya3
  • 47,440
  • 4
  • 68
  • 97
Abhi
  • 39
  • 1
  • 3
  • Is there any spaces in your line1? Many time this is due to having tabs in the code. Please check – Pygirl Feb 23 '20 at 11:19
  • 1
    This looks like the command itself for running your file from the menu is broken. It's sending an & symbol followed the file path as literal code, and Python complains because the & and the file path are not code. – kaya3 Feb 23 '20 at 11:26

1 Answers1

2

"Looks like your problem is that you are trying to run python test.py from within the Python interpreter, which is why you're seeing that traceback.

Make sure you're out of the interpreter, then run the python test.py command from bash or command prompt or whatever."

--- syntax error when using command line in python

just enter "quit()" in terminal and try again

like this

wulu
  • 46
  • 3
  • Yes, That worked well. When I use "run line in python terminal" command, VS Code starts python interpreter and it didn`t quit it after completion. I used exit() command in python interpreter window to end python session. – Abhi Feb 23 '20 at 11:55