Instantiate an object


Sorry to be a nuisance. It seems that my problem arises from the last three lines. I am not sure why.I get the message.

"Oops, try again. Did you create an instance of Triangle called my_triangle?"
I know mu code is not elegant

class Triangle(object):
    number_of_sides = 3
    def check_angles(self, angle1, angle2, angle3, total ):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3 =angle1+angle2+ angle3
        if total==180:
            return true
            return false
my_triangle = Triangle(35, 55, 90)
print my_triangle.number_of_sides
print my_triangle.check_angles(35, 55, 90)


what happened to your __init__() method?


hmm, good question. I followed the instruction to the letter and just created a variable, my_triangle, and set it to the instance Triangle and then the print statements
Thank you for your help.
One more silly/basic question Is there a difference, in the results or syntax terms,between (self.angle1+self.angle2+self.angle3) and (angle1+angle2+angle3)?


but your __init__() method should be present from earlier exercise

well, if you wanted to do:

def check_angles(self, angle1, angle2, angle3):
     if angle1 + angle2 + angle3 == 180:
         return True
         return False

you will need to provide the angles again when calling the check angle methods:

print my_triangle.check_angles(35, 55, 90)

however, if you use __init__() correctly you only have to supply the angles at initialization and then you can just call check_angles:

print my_triangle.check_angles()

this is much better, what if we had more methods like check_angles, providing the angles every time you want to call a method.

remember, programming should be DRY (don't repeat yourself)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.