我是python新手,我正在尝试编写类RectangleCollection。类Rectangle是给定的,我需要编写类RectangleCollection部分。 类RectangleCollection有一个列表实例变量rectangles,它最初应该引用一个空列表。 get_same_area矩形接受一个数字作为参数,并返回矩形列表中具有该区域的所有矩形的列表。在
class Rectangle:
""" A rectangle with a width and height. """
def __init__(self, w, h):
""" (Rectangle, number, number)
Create a new rectangle of width w and height h.
>>> r = Rectangle(1, 2)
>>> r.width
1
>>> r.height
2
"""
self.width = w
self.height = h
def area(self):
""" (Rectangle) -> number
Return the area of this rectangle.
>>> r = Rectangle(10, 20)
>>> r.area()
200
"""
return self.width * self.height
以下是我所做的:
^{pr2}$但是为了得到相同的面积,我总是错的。。 我不知道我做错了什么。请帮忙
我想是因为钢筋混凝土矩形给我r1,r2,r3地址,而不是区域。我应该得到[200300200]但是我有地址。我想这就是为什么我总是弄错。。我怎样才能解决这个问题?在
你的代码中有一个印刷错误。它应该是:
extend
不是{或者简单地说:
^{pr2}$CSC108对吗?类中的这个函数不是要求你返回True或False,而是要求你调用这个函数来得到一个矩形的列表,矩形的面积是200
使用
filter
函数只取面积为number
的矩形如何相关问题 更多 >
编程相关推荐