About member variable in Triangle class Class It Up


#1



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


Oops, try again.
It looks like your Triangle class doesn't have a number_of_sides member variable.


class Trianle:
    
    number_of_sides = 3
    
    def __init__(self,side1,side2,side3):        
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3
        
    def check_angles(self):
        if self.angle1+self.angle2+self.angle3 == 180 :
            return True
        else:
            return False


#2

Put the number_of_sides variable as the last line of the init function and let it have the same indentation level as def :slight_smile:


#3

I did below code as you say

class Triangle:

def __init__(self,number_of_sides):
self.number_of_sides = 3

def check_angles(self):
    if self.angle1+self.angle2+self.angle3 == 180 :
        return True
    else:
        return False

my_triangle = Triangle(3)

Still got error:
File "python", line 4
self.number_of_sides = 3
^
IndentationError: expected an indented block


#4

Don't call it with self. just write it like number_of_sides = 3 Plus, why did you remove your other arguments and self.'s? Keep those just add number_of_sides as it is.


#5

class Triangle:

def __init__(self,side1,side2,side3,number_of_sides):
    self.side1 = side1
    self.side2 = side2
    self.side3 = side3
self.number_of_sides = 3


def check_angles(self):
    if self.angle1+self.angle2+self.angle3 == 180 :
        return True
    else:
        return False

I did this after that I got this error in bottom popup
" Oops, try again.
Your code threw the following error when it tried to check Triangle: global name 'Triangle' is not defined"

Error in terminal window :
Traceback (most recent call last):
File "python", line 1, in
File "python", line 7, in Triangle
NameError: name 'self' is not defined


#6

This line of yours should be like this

class Triangle(object):


#7

I did it like below

class Triangle(object):

def __init__(self,side1,side2,side3,number_of_sides):
    self.side1 = side1
    self.side2 = side2
    self.side3 = side3
self.number_of_sides = 3

def check_angles(self):
    if self.angle1+self.angle2+self.angle3 == 180 :
        return True
    else:
        return False

Still got the error :
" Oops, try again.
Your code threw the following error when it tried to check Triangle: global name 'Triangle' is not defined"


#8
class Triangle(object):
    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

Indent your code like this and it should work. There is no problem with your code apart from indentation


#9

is it compulsory to inherit every class via object?


#10

No, that is used to pass a class if you have no object to give to the class.
but generally super classes like the triangle one can just be created like this,

class Triangle:


#11

You are really awesome thanx


#13

3 posts were split to a new topic: Member variable, class it up


#14