16. Class It Up question


#1



https://www.codecademy.com/en/courses/python-intermediate-en-WL8e4/3/2?curriculum_id=4f89dab3d788890003000096#


Make sure to pass self as an argument to your check_angles method!

I received this error message but in my code I do pass self as an argument to my check_angles method. Confused, help please!


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 sum(self.angle1, self.angle2, self.angle3) == 180:
            return True
        else:
            return False


#2

The number of sides variable ,

should be after this function,

and should be on the same indentation level as this line,


#3

Why "should be on the same indentation level as this line" ?

class Triangle(object):
    number_of_sides = 3
    def __init__(self,angle1,angle2,angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3

works great

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

easy as send two bits!

if seriously you code must look like this:

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 sum([self.angle1, self.angle2, self.angle3]) == 180:
            return True
        else:
            return False

Why sum([self.angle1, self.angle2, self.angle3]) ? Check out this https://docs.python.org/2/library/functions.html#sum


#4

This is what I meant,

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

sum([self.angle1, self.angle2, self.angle3]) is just a shorter way of writing self.angle1 + self.angle2 + self.angle3.


#5

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