如何使用类中的值执行求和(加法)?

时间:2015-12-14 00:45:11

标签: python class

我有我给过的代码:

class rectangle(shape):
    """A new class 'rectangle' that will inherit functions from the class
    'shape'.
    """

    def __init__(self, width, height):
        """A constructor function for the class.
        """
        self.name = "rectangle"
        self.area = width * height

r = rectangle(2, 3)

r.printArea()


class triangle(shape):
    """A new class 'triangle' that will inherit functions from the class
    'shape'.
    """

    def __init__(self, width, height):
        """A constructor function for the class.
        """
        self.name = "triangle"
        self.area = (width * height) / 2.0

t = triangle(3, 4)

t.printArea()

我无法改变上述任何一项。我的任务是建立在这门课上:

def sumAreaOfShapes(shapeList):
    """The sum of all the areas of the shapes in the list.
    """
    addition = str(t.getArea()) + str(r.getArea())
    return addition

print sumAreaOfShapes([rectangle(1.5, 7), triangle(7, 12)])

现在我必须在类之外将矩形和三角形的面积相加。我该怎么做呢?我的尝试是完全错误的,我真的不知道我在做什么:

def sumAreaOfShapes(shapeList):
"""The sum of all the areas of the shapes in the list.
"""
addition = (float(t.getArea()))+(float(c.getArea()))+(float(r.getArea()))
return addition

print sumAreaOfShapes([circle(100), rectangle(100, 100), triangle(100, 100)])

所有这一切实际上是将一个数字贴在另一个数字的末尾。

编辑:我稍微接近我正在寻找的东西,但它仍然没有用。

{{1}}

我现在得到了一个答案,但它大概是90.00,所以它实际上是对班级中的值进行求和,而不是来自我的打印。

1 个答案:

答案 0 :(得分:2)

使用带有生成器表达式的bulit-in函数if(aLetter.getText().equals("Letter trying to guess"){ "Show blank letter" } else{ "Hangmans death" }

sum