Class It Up


#1
What am I missing with the variable number_of_sides?

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

#2

Hi @coldweb,

This is indented too much, making it part of the __init__ method …

    number_of_sides = 3

As a result, number_of_sides becomes a local variable within that method, rather than a class variable.

Remove a level of indentation from that line, so that it becomes indented to the same degree as the __init__ method header.

Also, it would be better programming style for it to be located right after this line …

class Triangle(object):

#3

Good Afternoon @appylpye

Here’s my updated code:

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

When I look at this, the number_of_sides variable is nested in the Triangle object, but it still doesn’t work.


#4

Check all the indentation. Consider what needs to be changed for the check_angles method to be part of the Triangle class definition.


#5

Awesome thanks! @appylpye


#6