【问题标题】:How to use this code in python 3 version? [duplicate]如何在 python 3 版本中使用此代码? [复制]
【发布时间】:2014-06-17 18:27:07
【问题描述】:

我想在 python 3 中运行此代码,但我不能。每当我尝试运行代码时,我都会收到无效的语法错误。

age = 20
name = 'Swaroop'
print '{} was {} years old when he wrote this book'.format(name, age)
print 'Why is {} playing with that python?'.format(name)

请帮帮我。

谢谢。

【问题讨论】:

标签: python methods syntax format


【解决方案1】:

print 函数调用周围加上括号。

print('{} was {} years old when he wrote this book'.format(name, age))
print('Why is {} playing with that python?'.format(name))

在 Python2 中,print 是一个语句,不需要括号。

In Python3, print is a function,因此它的参数需要括号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-22
    • 2011-03-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2011-12-08
    • 1970-01-01
    相关资源
    最近更新 更多