17. Instantiate an Object - Help Needed


#1

Keeps returning an error saying "
Oops, try again. Did you create an instance of Triangle called my_triangle?"

The console error says "Traceback (most recent call last):
File "python", line 18, in
File "python", line 3, in init
AttributeError: 'int' object has no attribute 'self'"

I'm going to guess I'm missing something very obvious here, but it is currently about 4am...

class Triangle(object):
    def __init__(self, angle1, angle2, angle3):
        angle1.self = angle1
        angle2.self = angle2
        angle3.self = angle3
        
    number_of_sides = 3
    
    def check_angles(angle1, angle2, angle3):
        sum_of_angles = 0.0
        sum_of_angle = sum(angle1, angle2, angle3)
        
        if sum_of_angle == 180:
            return True
        else:
            return False

my_triangle = Triangle(90,10,80)
print my_triangle.number_of_sides
print my_triangle.check_angles()

#2

sum() is list method.

sum([angle1, angle2, angle3])

#3

I hope this in not to late.
line 3 shouldn't be self.angle1 = angle1

The same with the other two parameters


#4

I think you meant, shouldn't be,

angle1.self

but should be,

self.angle1

This is another area of concern. The instance of this class has this method. There is no need to pass in any angles, only itself.

def check_angles(self):
    return sum([self.angle1, self.angle2, self.angle3]) == 180

That way the method call lower down in your code will work.

print my_triangle.check_angles()

#5

Yea, you were right...


#6

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