【问题标题】:Multi-line print statements with variables: Python 2 versus Python 3带变量的多行打印语句:Python 2 与 Python 3
【发布时间】:2017-07-19 02:25:57
【问题描述】:

我正在从 Zed A. Shaw 的 Learn Python the Hard Way 那里学习 Python,这本书强烈推荐 Python 2 而不是 Python 3。然而,到目前为止,我一直在做Python 3(以便了解两个版本之间的差异)。下面是练习8中的代码,

formatter = "%r %r %r %r"

...

print(formatter % (
   "I had this thing.",
   "That you could type up right.",
   "But it didn't sing.",
   "So I said goodnight."
   )
)

适用于 Python 2,但不适用于 Python 3,其中 PowerShell 在 print 语句的第一行声明语法错误。

我对 Python 2 和 Python 3 之间关于print 语句的大部分区别是正确的吗?上面的代码在 Python 3 中看起来如何?

【问题讨论】:

  • 你可以试试Learn Python 3 the Hard Way,这本书的 Python 3 更新版本。
  • 话虽如此,您的代码示例在 Python 3.5 中对我来说可以正常工作。 print 之前是否有未闭合的括号?
  • 你是对的。确实,打印之前有一个未闭合的括号。

标签: python-3.x variables printing syntax python-2.x


【解决方案1】:

问题是由前面的 print 语句中的未闭合括号引起的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多