Hello every one, when I run this code, I get a attribute error. I used two diffrent ways to find the area of pyramid but the second way is not working. I do not know how to solve this problem. If you know how to solve this problem, please help me. thanks a lot!
class Rectangle:
def __init__(self, length, width) -> None:
self.length = length
self.width = width
def area(self) -> float:
return self.length * self.width
def perimeter(self) -> float:
return 2 * self.length + 2 * self.width
class Square(Rectangle):
def __init__(self, length) -> None:
super(Square, self).__init__(length, length)
class Cube(Square):
def surface_area(self) -> float:
face_area = super(Square, self).area()
return face_area * 6
def volume(self) -> float:
face_area = super(Square, self).area()
return face_area * self.length
class Triangle:
def __init__(self, base, height) -> None:
self.base = base
self.height = height
def tri_area(self) -> float:
return 0.5 * self.base * self.height
class RightPyramid(Square, Triangle):
def __init__(self, base, slant_height) -> None:
self.base = base
self.slant_height = slant_height
super().__init__(self.base)
def area(self) -> float:
base_area = super(Square, self).area()
perimeter = super(Square, self).perimeter()
return 0.5 * perimeter * self.slant_height + base_area
def area_2(self) -> float:
base_area = super(Square, self).area
triangle_area = super(Triangle, self).tri_area()
return triangle_area * 4 + base_area
print(RightPyramid.__mro__)
pyramid = RightPyramid(3, 6)
print(pyramid.area())
print(pyramid.area_2())
I notice you are using inheritance all the way through your classes.
While I agree a Square is a subclass of Rectangle, a Cube in not a subclass of Squares, it is composed of Squares (hint, read up about composition vs inheritance).
Also since python 3, super()
does not need arguments in most cases.
There are a couple of problems in your code:
RightPyramid.__init__
does one super().__init__(self.base)
, so how should super(Triangle, self).tri_area()
access the height
? area_2
, you're accessing super(Square, self).area
, not actually calling it. This is what I can offer you for your RightPyramid
problem.
class Rectangle:
def __init__(self, length, width) -> None:
self.length = length
self.width = width
def area(self) -> float:
return self.length * self.width
def perimeter(self) -> float:
return 2 * self.length + 2 * self.width
class Square(Rectangle):
def __init__(self, length) -> None:
super(Square, self).__init__(length, length)
class Triangle:
def __init__(self, base, height) -> None:
self.base = base
self.height = height
def tri_area(self) -> float:
return 0.5 * self.base * self.height
class RightPyramid:
def __init__(self, base, slant_height) -> None:
self.base = Square(base)
self.side = Triangle(base, slant_height)
def area(self) -> float:
base_area = self.base.area()
perimeter = self.base.perimeter()
return 0.5 * perimeter * self.side.height + base_area
def area_2(self) -> float:
base_area = self.base.area()
triangle_area = self.side.tri_area()
return triangle_area * 4 + base_area
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.