17 - what is wrong?


#1

class Triangle(object):
number_of_sides = 3
def init(self,angle1,angle2,angle3):
angle1 = self.angle1
angle2 = self.angle2
angle3 = self.angle3
def check_angles(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()

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


#2

You've got the assignments in your init function backwards. You need to do

self.angle1 = angle1

etc.


#3

class Triangle(object):
number_of_sides = 3

def __init__(self, angle1, angle2, angle3):
    self.angle1 = angle1
    self.angle2 = angle2
    self.angle3 = angle3

    def check_angles(self):
        if self.angle1 + self.angle2 + self.angle3 == 180:
            return True
        else:
            return False
    my_triangle = (90, 30, 60)
    print my_triangle.number_of_sides
    print my_triangle.check_angles()

gives the error "Oops, try again. It looks like my_triangle isn't an instance of the Triangle class."

What did I do wrong?


#4

Hi
To create object you must use following syntax:

Object_name = Class_name(arguments)


#5

From what you have there your def check_angles(self): method is part of your __init__ method.

You should have something like the following,

class Triangle(object):
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
    def check_angles(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 You do not have something to handle this
print my_triangle.check_angles()

#6

That worked. There was a lot of other stuff wrong but I fixed those too. Thanks!


#8

What's wrong here I get the erroe code:"Oops, try again. It looks like my_triangle isn't an instance of the Triangle class."

class Triangle(object):

number_of_sides = 3

def init(self,angle1,angle2,angle3):

self.angle1 = angle1

self.angle2 = angle2

self.angle3 = angle3

def check_angles(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()