<p>在print语句中打印汽车的价格,执行时会出现如下错误</p>
<blockquote>
<p>only strings can be concatenated in print stement</p>
</blockquote>
<pre><code>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()
</code></pre>
<p>他们花了2000卢比买了一辆木车,预计将投入生产。你知道吗</p>
<p>如消息所示,您只能将字符串连接到另一个字符串,因为要连接的值必须是<em>相同的</em>类型。你知道吗</p>
<p>更好的方法:</p>
<pre><code> print("They brought a car {} for rupees {}".format(x1.car, x1.price))
</code></pre>