在print语句中打印汽车的价格,执行时会出现如下错误
only strings can be concatenated in print stement
class Board:
def __init__(x1,car,price):
x1.car=car
x1.price=price
def init1(x1):
print("They brought a car "+x1.car +"for rupees" x1.price) ### this is where I need the print of price .
y=Board("xylo",2000)
y.init1()
他们花了2000卢比买了一辆木车,预计将投入生产。你知道吗
如果不需要进一步格式化,也可以将字符串和整数作为单独的参数传递给
print
:整数将根据需要转换为字符串,不同的参数将被打印出来,默认情况下用空格隔开-您可以使用print的
sep=
关键字参数来更改这一点。你知道吗如果您使用的是python3.6+,那么可以使用
f-string
。你知道吗如消息所示,您只能将字符串连接到另一个字符串,因为要连接的值必须是相同的类型。你知道吗
更好的方法:
相关问题 更多 >
编程相关推荐