Build a Polygon Area Calculator - Build a Polygon Area Calculator

Steps 14-20 do not check out with this code. Notably, step 20 should return (and does) return 6, but it does not count as valid:

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def set_width(self):
        return self.width

    def set_height(self):
        return self.height

    def get_area(self):
        return self.width * self.height

    def get_perimeter(self):
        return 2 * (self.width + self.height)

    def get_diagonal(self):
        return ((self.width ** 2 + self.height ** 2) ** 0.5)

    def get_picture(self):
        if self.width > 50 or self.height > 50:
            return 'Too big for picture.'
        picture = ""
        for _ in range(self.height):
            for _ in range(self.width):
                picture += '*'
            picture += '\n'
        return picture

    def get_amount_inside(self, shape):
        x_count = (self.width - self.width % shape.width) / shape.width
        y_count = (self.height - self.height % shape.height) / shape.height
        if x_count == 0 or y_count == 0:
            return 0
        return int(x_count) * int(y_count)

    def __str__(self):
        return f"Rectangle(width={self.width}, height={self.height})"


class Square(Rectangle):
    def __init__(self, side):
        super().__init__(side, side) #width, height are now side, side

    def set_side(self, side):
        super().set_width(side)
        super().set_height(side)

    def __str__(self):
        return f"Square(side={self.width})"


print(Rectangle(15,10).get_amount_inside(Square(5))) # console shows 6

Challenge Information:

Build a Polygon Area Calculator - Build a Polygon Area Calculator

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-polygon-area-calculator/5e444147903586ffb414c94f.md at main · freeCodeCamp/freeCodeCamp · GitHub

If I try this test:

new_rectangle = Rectangle(3, 6)        
new_rectangle.set_width(7)

I get this error:

Traceback (most recent call last):
  File "main.py", line 55, in <module>
TypeError: Rectangle.set_width() takes 1 positional argument but 2 were given

Hi @MrToblerone,

It looks like what are supposed to be setters are actually getters:

If you test your code like this:

rect = Rectangle(10, 5)
print(rect.get_area())
rect.set_height(3)
print(rect.get_perimeter())
print(rect)
print(rect.get_picture())

sq = Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)
print(sq.get_picture()) 

rect.set_height(8)
rect.set_width(16)
print(rect.get_amount_inside(sq))

You will see this error in the console:

Traceback (most recent call last):
  File "main.py", line 58, in <module>
TypeError: Rectangle.set_height() takes 1 positional argument but 2 were given

Happy coding

That was a big detail I missed, thank you! After fixing the setters, now the only step that doesn’t check out is 16. Here’s the change I made to my code:

def set_width(self, width):
    self.width = width

def set_height(self, height):
    self.height = height

Did I get the set_side method wrong?

Have you tested setting the side to see if it works?

16. An instance of the Square class should have a different string representation after setting new values by using .set_width() or set_height().

sq = Square(9)
print(sq)
sq.set_width(4)
print(sq)
sq.set_height(6)
print(sq)
Square(side=9)
Square(side=4)
Square(side=4)

I’ve run the test code dhess posted before with a set_side() line in between to see the changes, like so:

sq = Square(9)
print(sq.get_area()) # 81
sq.set_side(4)
print(sq.get_diagonal()) # 5.656854249492381
sq.set_side(2)   # added test line
print(sq.get_diagonal()) # 2.8284271247461903
print(sq)
print(sq.get_picture()) 

And it returns the correct result of the diagonal of the new square, with a side length of 2. As for the string representation, the get_picture() call returns:

**
**

But I’ve noticed that with the new test lines, the console returns different results for the get_amount_inside(sq) at the end, now returning 32, when the previous returned result was 8.

Edit: now I get it, after adding the test just like the message above, the set_height(6) doesn’t update the square’s side length because the string representation checks only for the square’s width, not height. Changing it to return f"Square(side={self.side})" throws an AttributeError, however.

Edit 2: I solved it in the end by changing the set_width() and set_height() to make sure they check the class name and update width and height correctly.

In the future, try to focus on exactly what is mentioned in the test:

16. An instance of the Square class should have a different string representation after setting new values by using .set_width() or set_height().

So you should be testing .set_width() and set_height() to see if they work.

“String representation” refers to the output of the __str__(self): method, not get_picture, although both should change in any case. The language there is a bit confusing " get_picture: Returns a string that represents the shape"

See user story 3 as well:

  1. If an instance of a Rectangle is represented as a string, it should look like: Rectangle(width=5, height=10).

Glad you got it!