我得到了一些代码:
import math
class shape(object):
"""Shape is an base class, which is largely intended to be abstract, that
is only used as a parent class to other classes wich define specific shapes
and inherit the shape class' fuctions getArea() and printArea().
"""
def __init__(self):
'''constructor function for the shape class
'''
self.area = None
self.name = "shape"
def getArea(self):
'''returns the area of the shape, which is defined in the constructor
'''
return self.area
def printArea(self):
'''prints a statement identifying the shape by its name and giving the
area of the shape. Returns None.
'''
print "The area of this " + self.name + " is: " + str(self.area)
return None
以上这些我都改不了。我的任务是在那门课上继续学习:
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)])
我现在得到了一个答案,但它大约是90.00,所以它实际上是从类中求和的值,而不是从我的打印。你知道吗
将bulit in函数
sum
与生成器表达式一起使用:相关问题 更多 >
编程相关推荐