显示框的属性

Display Attributes of a Box

我试图让我的程序输出类似这样的输出:

长度:3

宽度:4

身高:5

颜色:红色

但是我目前收到此错误:

回溯(最后一次调用): 文件“C:/Users/chaos/OneDrive/Documents/Python/Box_Assignment.py”,第 30 行,位于 打印(b1.get_attributes) AttributeError: 'tuple' 对象没有属性 'get_attributes'

让我知道你的想法,我的代码在下面。

class Box():


    def __init__(self,Length,Width,Height,Color):
        self.length=Length
        self.width=Width
        self.height=Height
        self.color=Color

    def __str__(self):
        return 'length:'+(self.length)+'width:'+(self.width)+'height:'+(self.height)+'color:'+str(self.color)

    def get_attributes(self):
        attributes = self.length,self.width,self.height,self.color
        return attributes
        


b1=(3,4,5,'Red')

b2=(5,6,7,'Blue')

print(b1.get_attributes)

(移动评论回答)

创建对象时,包含 class 名称:

b1 = Box(3,4,5,'Red')

调用class方法时,包括括号:

print(b1.get_attributes())