0
2.0kviews
Write a Python class named polygon with two methods input sides and display sides. Inherit a Class Triangle from polygon and calculate area of triangle.
1 Answer
0
148views

Code:-

class Polygon:
    def __init__(self,no_of_sides):
        self.n=no_of_sides
        self.sides=[0 for i in range(no_of_sides)]
    def input_sides(self):
        self.sides=[float(input("Enter side"+str(i+1)+':'))
                    for i in range(self.n)]
    def display_sides(self):
        for i in range(self.n):
            print("Side",i+1,"=",self.sides[i])
class Triangle(Polygon):
    def __init__(self):
        Polygon.__init__(self,3)
    def findArea(self):
        a,b,c=self.sides
        s=(a+b+c)/2
        area=(s*(s-a)*(s-b)*(s-c))**0.5
        print("The area of the triangle is %0.2f"%area)
t=Triangle()
t.input_sides()
t.display_sides()
t.findArea()

Output:-

enter image description here

Please log in to add an answer.