Here is my line of code:

class Rectangle(object):
    def __init__(self,x,y):

        def Area(self):

            return self.x*self.y

        def Perimeter(self):

            return self.x*2+self.y*2

def main():
    print "Rectangle a:"
    a = Rectangle(5, 7)
    print "area:      %d" % a.area
    print "perimeter: %d" % a.perimeter

    #print ""
    #print "Rectangle b:"
    #b = Rectangle()
    #b.width = 10
    #b.height = 20
    #print b.getStats()

if __name__ == "__main__":

What I can't figure out is how to pass the attribute of area. Any help or examples of what to do will be greatly appericated.

Edited 3 Years Ago by lewisrs89

You defined your methods using the names Area and Perimeter with a capital A and P respectively. You tried to call them as area and perimeter with a lower case a and p. Since Python is case sensitive, that does not work.

Also: If you want to call a function or method you need to use parentheses, like this: a.Area(). Without the parentheses, you only get is a reference to the method without calling it.

You also have to do your indentations properly:

class Rectangle(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def area(self):
        return self.x*self.y

    def perimeter(self):
        return self.x*2 + self.y*2

def main():
    print("Rectangle a:")
    a = Rectangle(5, 7)
    print("area:      %d" % a.area())
    print("perimeter: %d" % a.perimeter())

    print("Rectangle b:")
    b = Rectangle()
    b.width = 10
    b.height = 20

if __name__ == "__main__":

Used print() so code will work with Python2 and Python3.

Edited 3 Years Ago by bumsfeld

This article has been dead for over six months. Start a new discussion instead.