Here is my line of code:

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 ""
    #print "Rectangle b:"
    #b = Rectangle()
    #b.width = 10
    #b.height = 20
    #print b.getStats()

if __name__ == "__main__":
    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.

Recommended Answers

All 2 Replies

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("")
    print("Rectangle b:")
    b = Rectangle()
    b.width = 10
    b.height = 20
    print(b.getStats())
    '''

if __name__ == "__main__":
    main()

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.