Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Code
#1
Hello everyone, need some help with my code below. The objective of the code is to take input for 3 sides of a triangle from the user and print the area. I am trying to do it using classes but the area keeps showing as zero. Could you please let me know what in the code needs to be fixed

class area_input:
    def __init__(self):
        self.a = int(input("enter side 1"))
        self.b = int(input("enter side 2"))
        self.c = int(input("enter side 3"))

class area_cal(area_input):
    def __init__(self,*args):
        super(area_cal,self).__init__(*args)
        
    
    def __str__(self):
        self.s = (self.a+self.b+self.c)/2
        self.area = (self.s*(self.s-self.a)*(self.s-self.b)*(self.s-self.c))**0.5
        return "Area is %f" %(self.area)
Reply
#2
The part that calculates the area works just fine; however, there is an error in line 9 that keeps the code from running. Obviously, you modified the code for posting and removed the original error when you did. My guess is that the problem was in the original __init__ function on line 2 that you changed for posting, or the error was somewhere in the code you cut out.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020