Inheritance


#1



Traceback (most recent call last):
File "python", line 33, in
TypeError: unbound method print_sides() must be called with Equilateral instance as first argument (got nothing instead)

Oops, try again. Your code looks a bit off. See the console window for the full error message.


class Triangle:
    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
            

my_triangle = Triangle(90,30,60)

print my_triangle.number_of_sides
print my_triangle.check_angles()
        
class Equilateral:
    angle = 60
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = self.angle
        self.angle2 = self.angle
        self.angle3 = self.angle
    def print_sides(self):
        print self.angle
        
print my_triangle.number_of_sides
print my_triangle.check_angles()
equilateral_my = Equilateral
print equilateral_my.print_sides()


#2

You are extraordinarily close, please look at my comments on your code:

class Triangle: //should be class Triangle(object):
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
    number_of_sides = 3 
    //remove this line    
    //remove this line
    def check_angles(self):
        if self.angle1 + self.angle2 + self.angle3 == 180:
            return True
        else:
            return False

my_triangle = Triangle(90,30,60) //move line to bottom

print my_triangle.number_of_sides //move line to bottom
print my_triangle.check_angles() //move line to bottom
        
class Equilateral: //should be class Equilateral(Triangle):
    angle = 60
    def __init__(self, angle1, angle2, angle3): //don't need angle1, angle2, or angle3
        self.angle1 = self.angle
        self.angle2 = self.angle
        self.angle3 = self.angle
    def print_sides(self): //delete this line
        print self.angle //delete this line
        
print my_triangle.number_of_sides //delete this line
print my_triangle.check_angles() ; //delete this line
equilateral_my = Equilateral //delete this line
print equilateral_my.print_sides() //delete this line

#3

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