Build a Polygon Area Calculator - Build a Polygon Area Calculator

Tell us what’s happening:

I’ve tried for serveral hints, but 4 steps still failed. The steps are :

14 An instance of the Rectangle class should have a different string representation after setting new values.

17 The .get_picture() method should return a different string representation of a Rectangle instance.

19 The .get_picture() method should return the string Too big for picture. if the width or height attributes are larger than 50.

20 Rectangle(15,10).get_amount_inside(Square(5)) should return 6.

Your code so far

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    
    def set_width(self, width):
        return self.width
    
    def set_height(self, height):
        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):
        picture = ''
        rows = self.height
        stars = '*' * self.width
        if self.width > 50 or self.height > 50:
            return 'Too big for picture.'
        else:
            for i in range(rows):
                picture += f"{stars}\n"
        return picture
    
    def get_amount_inside(self, shape):
        self.shape = shape
        if self.shape.width <= self.width and self.shape.height <= self.height:
            if isinstance(self.shape, Square):
                x = self.width // self.shape.height
                # print(f"x - {x}")
                y = self.height // self.shape.height 
                # print(f"y - {y}")
                return x * y
                    
            else:
                x = self.width // self.shape.width
                y = self.height // self.shape.height 
                # print(f"y - {y}")
                lkm = min(x,y)
                return lkm
        else:
            return 0

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

class Square(Rectangle):
    def __init__(self, side):
        self.width = side
        self.height = side
        self.side = side
    
    def set_width(self, new_width):
        self.side = new_width
    
    def set_height(self, new_height):
        self.side = new_height
    
    def set_side(self, side):
        self.side = side

    def get_picture(self):
        picture = ''
        rows = self.side
        stars = '*' * self.side
        if self.side > 50 or self.side > 50:
            return 'Too big for picture.'
        else:
            for i in range(rows):
                picture += f"{stars}\n"
        return picture

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

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))

Your browser information:

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

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

Please review how to create setters then use them in your __init__ method.

Understanding Object Oriented Programming and Encapsulation - What are Getters and Setters | Learn | freeCodeCamp.org