打印类型(x)SyntaxError:语法无效

时间:2018-08-20 09:35:30

标签: python python-3.x syntax-error

所以我开始学习python 3,我想在ubuntu上运行一个非常简单的代码:

print type("Hello World")
         ^
SyntaxError: invalid syntax

当我尝试在终端中使用命令python3 hello.py进行编译时,它给了我上面的错误,但是当使用python hello.py时(我认为这意味着要使用python 2而不是3)就可以了。在终端中使用python 3和2 shell时相同。

我似乎缺少了一些愚蠢的东西,因为我做了一些研究,但没有人遇到同样的问题。

3 个答案:

答案 0 :(得分:2)

在Python3中,print was changed从语句到函数(带有括号):

#Python2.x
print type("Hello World")
#Python3.x
print(type("Hello World")

答案 1 :(得分:1)

在Python 3.x中,print()是一个函数,而在2.x中则是一个语句。 Python 3中正确的语法为:

print(type("Hello World"))

答案 2 :(得分:0)

This is because from Python 3, print is a function, not a statement anymore.因此,Python 3仅接受:

print(type("Hello World"))
相关问题