Class It Up: False for valid triangle


#1

https://www.codecademy.com/courses/learn-python/lessons/introduction-to-classes/exercises/class-it-up?action=lesson_resume

It looks like your check_angles method doesn’t return True for valid triangle angle values.

I checked with different valid triangles and got True as result in all cases. I can’t find the bug in my code, if there is.



class Triangle(object):
  number_of_sides = 3
  def __init__(self, angle1, angle2, angle3):
    Triangle.angle1 = angle1
    Triangle.angle2 = angle2
    Triangle.angle3 = angle3
  def check_angles(self):
    if (self.angle1 + self.angle2 + self.angle3 == 180.00):
      return True
    else:
      return False

test_triangle = Triangle(180, 0, 0)
print test_triangle.check_angles()



#2

Use the instance object, self in the initializer.

self is the temporary instance context object. When you invoke the check_angles method on the instance, self refers to test_triangle.


#3

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