-2

我在 Visual Studio Professional 2013 RC 中运行 Python。我已经安装了Python 3.3 附带的 Python Tools for Visual Studio 。

我只是在做一个非常简单的打印语句,但它报告了语法错误。

print 'aaa'

另外,在我输入后Enter,解释器拒绝打印aaa出来。截图如下:

在此处输入图像描述

来自解释器的错误消息:

>>> print '1
... '
... 
Traceback (most recent call last):
  File "C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_py_repl.py", line 627, in run_one_command
    self.execute_item()
  File "C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_py_repl.py", line 601, in execute_code_work_item
    code = compile(self.current_code, '<stdin>', 'single', self.code_flags)
  File "<stdin>", line 1
    print '1
           ^
SyntaxError: EOL while scanning string literal
>>> a=1
>>> print a
Traceback (most recent call last):
  File "C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_py_repl.py", line 627, in run_one_command
    self.execute_item()
  File "C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_py_repl.py", line 601, in execute_code_work_item
    code = compile(self.current_code, '<stdin>', 'single', self.code_flags)
  File "<stdin>", line 1
    print a
          ^
SyntaxError: invalid syntax
>>> 

我曾经玩过 Python 2.7。为什么3.3会这样?

4

1 回答 1

9

print在 Python 3 中不再是语句,它是函数 print(),因此正确的语法是:

print('aaa')
于 2013-10-07T09:39:24.797 回答