%20%E4%B8%AD%E6%98%BE%E7%A4%BA%E2%80%9Cend%E2%80%9D%E5%8F%82%E6%95%B0%E7%9A%84%E8%AF%AD%E6%B3%95%E6%97%A0%E6%95%88.png)
我正在尝试使用 Visual Studio Code 运行这个 Python 程序:
# 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 = ' ')
但是在构建时我在控制台中收到以下输出和错误消息:
$ 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
这里可能存在什么问题?我该如何解决?
答案1
正如 @Eliah Kagan 指出的那样,发生此错误的原因是您的 VS Code 使用 Python 2 作为解释器。在 VS Code 中切换到 Python 3 解释器它应该可以工作。
PS 免费 PEP 风格提示:写print('something', end=' ')
。为参数提供默认值时,请避免在=
运算符周围留空格。