Instantiate an Object error


#1



https://www.codecademy.com/en/courses/python-intermediate-en-WL8e4/3/3?curriculum_id=4f89dab3d788890003000096#


Error Messages:

Traceback (most recent call last):
File "python", line 13, in
TypeError: init() should return None, not 'bool'

Oops, try again. Did you create an instance of Triangle called my_triangle?


This function is supposed to return a Boolean, not "None". What am I missing from the code?


class Triangle(object):
    check_angles = 0
    number_of_sides = 3
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
        check_angles = self.angle1 + self.angle2 + self.angle3
        if check_angles == 180:
            return True
        else:
            return False
my_triangle = Triangle(90, 30, 60)
print my_triangle.number_of_sides
print my_triangle.check_angles()


#2

check_angles should be a method.


#3

Changed my code to:

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(angle1, angle2, angle3):
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()

Error message now reads:

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

Is this because my_triangle.check_angles() needs the angles too? Sorry about indents. This program won't allow me to paste or edit the indentation.


#4

in __init__() you make your instance variable accessible to self, this means you can pass self as parameter for your check_angle method, and then you have access to your instance variable (angle1) through self (self.angle1)


#5

I am afraid that did not work.

print my_triangle.check_angles(self)

Produced this error:

Traceback (most recent call last):
File "python", line 14, in
NameError: name 'self' is not defined

I commented out the final print statement to see if I would get an error or not, and the entire assignment gave me credit for the completion. This is not proper for what is asked.


#6

if i create a class:

class Example:
    def __init__(self, length):
        self.length = length

length is an instance variable, since its different for every instance of our class look:

line_one = Example(100)

length is one hundred for our instance line_one. in our __init__() we make our instance variable (length) accessible to self (self.length), this means that if we make a method:

def PrintLength(self):
    print self.length

we can access length through self. look:

line_one.PrintLength()

#7

Following the logic of your example then, your "class Example:" is nearly identical to mine (less the object). So is the initialization statement.

I use the syntax "my_triangle = Triangle(90,30,60). Same pattern in yours because I am calling the class with the parameters of the angles (length in your example) inside. Still see no difference in your code versus mine.

Your print statement is inside of the method. mine is outside. Is this where it is supposed to be? I am thinking that the instructions want the result of the method, namely True or False:

3 - Print out my_triangle.check_angles()

Please advise.


#8

i was hoping that if you followed my example, you would realize that your check_angle method:

def check_angles(angle1, angle2, angle3):

has the wrong parameter, since you can access angle1 till angle3 through self, self is the only parameter needed in your check_angles method


#9

I missed that detail when I ran through your example. Sorry.

That solved the issue. Code runs fine now.

I am still shaky on this subject (as you can tell). Are their additional resources to look at that you would recommend? I want to get stronger in working with classes and methods.


#10

You can always check the official python doc (link), its a bit tricky at first, but learning to read the docs will really boost your skill

I liked sololearn, but you have to work your way through the course. You might be able to take a shortcut (they offer shortcuts, you have to answer an amount of questions with a limited number of attempts)


#11

Thank you so much. I made both of those sites favorites.


#12

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