Instantiate an Object


#1

Okay, so my code is below... There is obviously this error that came up but at the same time, I get the green Way to Go! Start next lesson on the bottom... What have I missed??

3
<bound method Triangle.check_angles of <builtin.Triangle instance at 0x7f0adec2e6c8>>
None

Way to go! Start Next Lesson

class Triangle():
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
        
    number_of_sides = 3
    
    def check_angles():
        if ((angle1 + angle2 + angle2) == 100):
            return true
        else:
            return false
            
my_triangle = Triangle(90,30,60)
print my_triangle.number_of_sides
print my_triangle.check_angles


#2

here:

print my_triangle.check_angles

calling a method requires parantheses, general syntax:

instanceName.methodName()

without the parantheses you just print the method itself, which isn't wrong, but not what you want


#3

Nope that can't be it because when I did that I got the following error:

Traceback (most recent call last):
File "python", line 17, in
TypeError: check_angles() takes no arguments (1 given)


#4

here:

def check_angles():

methods should always take self as parameter, and to then access instance variables inside the method use self.instanceName so for example self.angle1


#5

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

my_triangle = Triangle(90,30,60)
print my_triangle.number_of_sides
print my_triangle.check_angles(90,30,60)

this worked... thanks!


#6

the parameter should be self, then inside the object we can access our instance variable through self inside the method, this way, we don't have to supply the angles again when calling the check_angles method


#8

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