Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I need to write a Python Program to get the area of the triangle using the following method.

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

A method to take the length of the surfaces of a triangle from the user and should be specified in the parent class and a function to estimate the area should be defined in the subclass.

I have executed the below code, but not able to get the output:

class poly:

    def _init_(self,a,b,c):

        self.a = float(a)

        self.b = float(b)

        self.c = float(c)

a= input("a=")

b= input("b=")

c= input("c=")

class triangle(poly):

    def get_area(self):

        s = (a + b + c) / 2

        return (s*(s-a)*(s-b)*(s-c)) ** 0.5        

t = triangle(a,b,c)

print("area : {}".format(t.area()))

1 Answer

0 votes
by (108k points)

There are many mistakes on your code, I have made the changes, kindly go through the below code:

class poly:

    def __init__(self,a,b,c):

        self.a = float(a)

        self.b = float(b)

        self.c = float(c)

a= int(input("a="))

b= int(input("b="))

c= int(input("c="))

class triangle(poly):

    def __init__(self,a,b,c):

        super().__init__(a,b,c)

    def get_area(self):

        s = (a + b + c) / 2

        return (s*(s-a)*(s-b)*(s-c)) ** 0.5        

t = triangle(a,b,c)

print("area : {}".format(t.get_area()))

List of errors:

  • First, the __init__ has two underscores( _ ) and not one. 
  • Second, in python 3 input display a string so you need to typecast to int before doing arithmetic operations, else you will get TypeError. 
  • Third, you have not called the init method to reference the superclass. 

Want to become a Python Developer? Check out this insightful Python Certification course.

 

Related questions

0 votes
1 answer
asked Feb 12, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Jul 9, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
0 votes
4 answers
0 votes
1 answer

Browse Categories

...