使用“打印"时语法无效? [英] Invalid syntax when using "print"?

查看:46
本文介绍了使用“打印"时语法无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Python,甚至无法编写第一个示例:

I'm learning Python and can't even write the first example:

print 2 ** 100

这给出了 SyntaxError: invalid syntax

指向 2.

这是为什么?我使用的是 3.1 版

Why is this? I'm using version 3.1

推荐答案

那是因为在 Python 3 中,他们将 print statement 替换为 print 功能.

That is because in Python 3, they have replaced the print statement with the print function.

现在的语法或多或少与以前相同,但需要括号:

The syntax is now more or less the same as before, but it requires parens:

来自python 3 中的新功能"文档:

Old: print "The answer is", 2*2
New: print("The answer is", 2*2)

Old: print x,           # Trailing comma suppresses newline
New: print(x, end=" ")  # Appends a space instead of a newline

Old: print              # Prints a newline
New: print()            # You must call the function!

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Old: print (x, y)       # prints repr((x, y))
New: print((x, y))      # Not the same as print(x, y)!

这篇关于使用“打印"时语法无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆