Build a Polygon Area Calculator Project - Build a Polygon Area Calculator Project

Tell us what’s happening:

Please help me rectify my code as i am constantly stuck on the 15th test

Your code so far

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

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

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

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

    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.'

        pic = '*' * self.width + '\n'
        pic = pic * self.height
        return pic

    def get_amount_inside(self, ob):
        return self.get_area() // ob.get_area()


class Square(Rectangle):
    def __init__(self, s):
        super().__init__(s, s)

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

    def set_side(self, s):
        self.width = s
        self.height = s

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Build a Polygon Area Calculator Project - Build a Polygon Area Calculator Project

Take a look what’s printed by this example, and whether that makes sense:

s = Square(5)
print(s)
s.set_height(6)
print(s)
s.set_width(15)
print(s)

Square(side=5)
Square(side=5)
Square(side=15)

This what it is printing
I think it makes sense

Why changing the width changes what is printed, but changing height doesn’t? Remember this is the square, where width is technically the height and they shouldn’t be different.

But i have assigned them both ‘s’(side) in square
So what should I improve

That’s just when new Square instance is created, isn’t it?

Yes , but that is what is required right?
Else how I am supposed to calculate for the square

Consider again the example:

  • Square is created with side 5
  • Height is changed to 6, but square still appear as having side 5.
  • Width is changed to 15, square now appears as having side 15.

Problem is not when square is created, but when attempt to change is made.

So what should I change in my code?

can you find where the behaviour comes from on your own?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.