0

I'm trying to run this Python program with Visual Studio Code:

# This Python program must be run with
# Python 3 as it won't work with 2.7.

# ends the output with a <space>
print("Welcome to" , end = ' ')
print("GeeksforGeeks", end = ' ')

But while building I get following output and error messages in console:

$ python /home/mrrobot/Documents/Python/loop_while.py
  File "/home/mrrobot/Documents/Python/loop_while.py", line 5
    print("Welcome to" , end = ' ')
                             ^
SyntaxError: invalid syntax

What could be the problem here? How can I fix it?

screenshot of VS Code

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
Ajay Saini
  • 11
  • 1
  • 1
  • 5
    Does this answer your question? [Python script that runs in Windows throws "" error in Ubuntu](https://askubuntu.com/q/1164607) The specific error is different but the situation is pretty much the same. As the comments in the code say, the code is Python 3 and it does not work when run with a Python 2 interpreter. VS Code shows what interpreter you're using on the lower left of the window as Python 2. Furthermore, you're using [the `python` command, which in Ubuntu is still nearly always Python 2](https://askubuntu.com/q/1165360). Use `python3` instead and the program should work. – Eliah Kagan Aug 10 '20 at 07:49

1 Answers1

1

As @Eliah Kagan pointed out, the error occurs because your VS Code is using Python 2 as the interpreter. Switch to Python 3 interpreter in your VS Code and it should work.

P.S. Free PEP style tip: Write print('something', end=' '). When giving default values to parameters, avoid spacing around the = operator.