📌  相关文章
📜  print "hello world" ^ SyntaxError: Missing parentheses in call to 'print' (1)

📅  最后修改于: 2023-12-03 14:45:39.533000             🧑  作者: Mango

代码报错分析:Missing parentheses in call to 'print'

当你在 Python 2.x 版本中运行 print "hello world" 是没有问题的,但是在 Python 3.x 版本中这条语句就会报错:SyntaxError: Missing parentheses in call to 'print'。这条错误信息告诉我们在调用 print 函数时缺少了括号。

为什么在 Python 3.x 版本中需要加括号?

在 Python 3.x 版本中,print 不再是 Python 语言的语句,而是一个内置函数,需要像调用其他函数一样加上括号。这样做的好处是提高了 Python 语言的可读性和一致性,也更方便地支持多个参数的传递。

正确版本的 'hello world' 程序

在 Python 3.x 版本中,正确的 'hello world' 程序应该写成以下形式:

print("hello world")

在这个版本中,print 作为内置函数被调用,并传递了一个字符串参数 "hello world"。运行这个版本的程序,你将会看到输出了 "hello world" 这个字符串。

解决方案:给 print 添加括号

如果你需要在 Python 3.x 中使用 print,你需要在调用 print 函数的时候添加括号。例如,在向控制台输出 "Hello World!" 时,应该使用以下代码:

print("Hello World!")

print 添加括号让代码更规范和易读。