首页 > 解决方案 > 如何将我的属性类型之一更改为 int?

问题描述

对于一些背景信息,我试图让自己熟悉 Python 上的对象、类和属性。

class Plane:
def __init__(self,ModelName,brand,capacity):
    self.ModelName = ModelName
    self.brand = brand
    self.capacity = capacity

def intro_plane(self):
    print(
        "The name of the plane is " + self.ModelName + 
        ". It was released by " + self.brand +
        ". It has a capacity of " + self.capacity
        )
P1 = Plane("B737", "Boeing", 155)
P1.intro_plane()

我在 VsCode 上运行了我的代码,结果如下:

TypeError: can only concatenate str (not "int") to str

标签: pythonclassobjectattributes

解决方案


capacity是一个int,不是一个string。因此,当您添加print功能时,您需要更改它的类型。

". It has a capacity of " + str(self.capacity)

推荐阅读