Introduction to classes: 17: Instatiate an object


#1



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


Traceback (most recent call last):
File "python", line 13, in
File "python", line 3, in init
AttributeError: 'Triangle' object has no attribute 'angle1'

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


class Triangle(object):
    def __init__(self, angle1, angle2, angle3):
        angle1 = self.angle1
        angle2 = self.angle2
        angle3 = self.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(80, 40, 60)

print my_triangle.number_of_sides
print my_triangle.check_angles()


#2

These lines are reverse of what they need to be.

angle1, etc.

are parameters.

self.angle1, etc.

are instance variables.

self.angle1 = angle1

and so on.


#3

Thanks! Syntax is so unbelievably specific, and error messages relatively vague, I think I'm going to have to make every possible mistake before I learn Python without hitting a wall every time I try to write code!!


#4

Welcome to the club. Revel in mistakes as they are often the best learning devices. Learn to live with a little embarrassment and set aside pride. We are after all, human, and humans learn from mistakes (most of the time). Be sure to go easy on yourself and keep on plugging away!


#5

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