如何在python 3版本中使用此代码? [重复]

问题描述 投票:0回答:1

我想在 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个回答
1
投票

print
function 调用两边加上括号。

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

在Python2中,

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

在Python3中,

print
是一个函数,所以它的参数需要用括号括起来。

© www.soinside.com 2019 - 2024. All rights reserved.