Problem in Inheritance in review exercises


#1




It is giving error that global name 'angle' is not defined
although I have already defined the variable 'angle'


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)



class Equilateral(Triangle):
    angle = 60
    def __init__(self):
        self.angle1=angle
        self.angle2=angle
        self.angle3=angle
        
tri= Equilateral()       
print Equilateral.number_of_sides
print Equilateral.check_angles()


#2

from the instructions:

Create an init() function with only the parameter self, and set self.angle1, self.angle2, and self.angle3 equal to self.angle (since an equilateral triangle's angles will always be 60˚

equal to self.angle, you used angle

This is what is causing the error message.

You need to use self, member variables (angle) are automatically added to self. otherwise __init__() would also need angle as parameter beside self. This approach is not preferred.


#3

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