Issue
I'm writing a program to find the volume of a cylinder using inheritance. However, I'm getting the following error, and I'm not sure how to resolve it:
TypeError: __init__() missing 1 required positional argument: 'height'
Here is my code:
class Circle:
def __init__(self,radius,height):
self.radius = radius
self.height = height
class Cylinder(Circle):
def __init__(self,radius,height):
super().__init__(radius, height)
self.volume = 3.14 * (self.radius ** 2) * self.height
def set_volume(self):
print(self.volume)
radius = float(input("radius:"))
height = float(input("height:"))
cyl = Cylinder(radius,height)
print("Cylinder volume:", cyl.set.volume())
Solution
Your call to the __init__
of the superclass isn't correct. You need to pass in the radius and height parameters explicitly, rather than passing in the name of the superclass.
super().__init__(Circle)
should be
super().__init__(radius, height)
Answered By - BrokenBenchmark
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.