错误:1> 0并打印("是")

时间:2014-04-14 07:02:53

标签: python python-2.x

的Python:

1>0 and print("yes")

SyntaxError:' print'

中的语法无效

有人能说出原因吗?谢谢!

2 个答案:

答案 0 :(得分:10)

在Python 2中,print是一个语句,不能用作表达式。

要在Python 2中使用Python 3的打印功能,您需要先导入它:

from __future__ import print_function

<强>演示:

>>> 1>0 and print("yes")
  File "<ipython-input-2-0714eacbdec3>", line 1
    1>0 and print("yes")
                ^
SyntaxError: invalid syntax

>>> from __future__ import print_function
>>> 1>0 and print("yes")
yes

答案 1 :(得分:0)

在Python 2. * print是一个语句,你不能在(布尔)表达式中使用语句,因为它们不返回值。

相关问题