0

I don't know where to go from here. If a rectangle is created with an even area, the calculated area should print when the get_area method is called and the value of num_rectangles should increase by 1. If a rectangle is created with an odd area, the program should reply with a message stating area is not an even value.

class Rectangle:
    """
    This class represents a rectangle. It
    has a length and a width and is capable
    of computing its own area.
    """

    def __init__(self, length = 0, width = 0):
        """
        Initializes a rectangle with an optional
        length and width.
        """
        self.length = length
        self.width = width
        self.num_rectangles = num_rectangles +1

    def __repr__(self):
        """
        Returns a string representation of the
        rectangle.
        """
        return "rectangle with length: " + str(self.length) \
            + " and width: " + str(self.width)

    def get_area(self):
        """Returns the rectangle's area."""
        self.area = self.width * self.length
        if (self.area %2) == 0:
            return str(area)


r1 = Rectangle(2, 6)
print r1
print r1.get_area()


r2 = Rectangle(3, 5)
print r2
print r2.get_area()
Sociopath
  • 13,068
  • 19
  • 47
  • 75
Jonny
  • 27
  • 1
  • 6
  • Your code has several problems and it is not executable. I get where you want to arrive, but first, you have to clear up your mind! E.g.: _what `self.num_rectangles`? When is the variable `num_rectangles` initialized?_ – Gsk Nov 01 '19 at 09:24
  • I suggest to rethink what do you want from your class. It seems not a good idea to keep the number of rectangles in the Rectangle class. Maybe, you need a separate object that will contain all the created rectangles and has a method to calculate the number of rectangles with an odd area? By the way, the area is not guaranteed to be an integer. – ababak Nov 01 '19 at 09:35

3 Answers3

0

Define num_rectangles as class variable instead of instance variable and use if-else in get_area method:

class Rectangle:
    num_rectangles = 0

    def __init__(self, length = 0, width = 0):

        self.length = length
        self.width = width
        # Rectangle.num_rectangles += 1

    def __repr__(self):
        """
        Returns a string representation of the
        rectangle.
        """
        return "rectangle with length: " + str(self.length) \
            + " and width: " + str(self.width)

    def get_area(self):
        """Returns the rectangle's area."""
        area = self.width * self.length
        if (area %2) == 0:
            Rectangle.num_rectangles += 1
            return str(area) 
        else:
            return "area is not an even value."
Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • `num_rectangles` should only increment conditionally, that is, `if ((length * width) % 2) : Rectangle.num_rectangles += 1`. – user1016274 Nov 01 '19 at 09:26
  • The author wanted to keep track of the number of rectangles with an even area. And probably, get_area should not return None for uneven area. – ababak Nov 01 '19 at 09:26
  • @ababak OP wants to print the message only if area is not even so it could be done any way. However added that into return statement – Sociopath Nov 01 '19 at 09:30
0
class Rectangle:
    __num_rectangles = 0
    """
    This class represents a rectangle. It
    has a length and a width and is capable
    of computing its own area.
    """

    def __init__(self, length = 0, width = 0):
        """
        Initializes a rectangle with an optional
        length and width.
        """
        self.length = length
        self.width = width
        Rectangle.__num_rectangles += 1


    def __repr__(self):
        """
        Returns a string representation of the
        rectangle.
        """
        return "rectangle with length: " + str(self.length) \
            + " and width: " + str(self.width)

    def get_area(self):
        """Returns the rectangle's area."""
        self.area = self.width * self.length
        if (self.area %2) == 0:
            return str(self.area)
        else:
            print("Area isn't an even value")


r1 = Rectangle(2, 6)
print r1
r1.get_area()
print r1.get_area()


r2 = Rectangle(3, 5)
print r2
print r2.get_area()
Jonny
  • 27
  • 1
  • 6
-1

I think you are working with python 2.7!

check this question and its answers Creating a Rectangle class

Khyar Ali
  • 327
  • 2
  • 8
  • I think the original question was not about about the Python version and a general Rectangle class. – ababak Nov 01 '19 at 09:39