Instantiate an object HELP


#1

I do not understand why my code is not working. I suspect it is because of me not creating an "instance".
Below is my 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

And the console returns this error:
Oops, try again. Did you create an instance of Triangle called my_triangle?

someone please help!


#2

your suspension is right. You did not create a class instance, what is the struggle with creating a class instance?


#3

I don't honestly understand myself. I still don't fully get the instance bit.
instance = Class(args) is shown in the example.
but i don't get what is meant to go into the args spot. Is it the triangle or my_triangle?


#4

just like with normal functions:

def example(parameter1, parameter2):
    # function content

example("argument1", "argument2")

we supply arguments at function call to satisfy the function parameters

__init__() is known as a magic method, the magic bit here is that init is automatically called when you create a class instance

so, we need to supply arguments to satisfy our angle1, angle2 and angle3 parameters of init method.


#5

Thanks! Now I understand!


#6

I did it and I completed python! Woo Hoo!


#7

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