17. Instantiate an Object


#1




Traceback (most recent call last):
File "python", line 13, in
File "python", line 3, in init
AttributeError: 'Triangle' object has no attribute 'angle1'


Hi, I've tried different ways that I was hoping to fix it such as adding self.angle instead of angle, and putting self before 90 in my_triangle. Anyway, can't figure it out.


Replace this line with your code. 
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():
        if angle1 + angle2 + angle3 == 180:
            return True
        else:
            return False
            
my_triangle = Triangle(90, 30, 60)
print my_triangle.number_of_sides
print my_triangle.check_angles()


#2

You have "self." on the wrong side of your statements :smile:

edit: so it is searching for a value in self.angle1 to put into angle1, but self.angle1 doesn't exist yet :p


#3

Also, in light with what @trigonometrical said, make sure at your def check_angles() <<<< you include self


#4

"""class Triangle(object):
number_of_sides = 3
def init(self,angle1,angle2,angle3):
self.angle1 = angle1
self.angle2 = angle2
self.angl3 = angle3
def check_angle(self):
if self.angle1 + self.angle2 + self.angle3 == 180:
return True
else:
return False

my_triangle = Triangle(90, 30, 60)
print my_triangle.number_of_sides
print my_triangle.check_angles() """

Error message is
File "python", line 14, in
AttributeError: 'Triangle' object has no attribute 'check_angles'

""please assume indentation in this code , i don't know why editor is not supporting it"


#5

def check_angle(self):

You need a "s" at the end of angle to match your print statement.


#6

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