python - 如何将我的属性类型之一更改为 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
解决方案
capacity
是一个int
,不是一个string
。因此,当您添加print
功能时,您需要更改它的类型。
". It has a capacity of " + str(self.capacity)
推荐阅读
- c - 第二次 calloc 问题 - 在 C 上
- javascript - 快速排序问题 - 没有得到正确的结果
- docker - Cloud Build,maven 包,没有目标文件夹
- php - 尝试使用 With 时按数据透视表排序数据
- python - Python 用 selenium 切换浏览器焦点
- javascript - 是否可以测量溢出滚动的距离?
- azure-devops - 在 Azure DevOps 构建管道中发送电子邮件通知的最佳方式?不想使用“通知模块”
- python - Jupyter Notebook 导入问题
- c# - 单击带有在运行时创建的控件的按钮更改文本框的文本
- api - 如何清理我的 json 响应以使其更清洁