0

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.

Edited by lewisrs89

3
Contributors
2
Replies
17
Views
4 Years
Discussion Span
Last Post by bumsfeld
0

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.

1

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.

Edited by bumsfeld

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.