Build a Polygon Area Calculator - Build a Polygon Area Calculator

Tell us what’s happening:

Hi!
Code is failing on step 20: Rectangle(15,10).get_amount_inside(Square(5)) should return 6.

My code is definitely returning 6 with this input. Every other test passes - when F12ing the assertion error states 0 != 6. Not sure exactly what the issue is.

Thanks in advance

Your code so far

import math
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height

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

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

    def get_area(self):
        return self.area

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

    def get_diagonal(self):
        square = (self.width ** 2) + (self.height **2)
        self.diagonal = math.sqrt(square)
        return self.diagonal
    
    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_obj):
        shape_area = 0
        if type(shape_obj) == Rectangle:
            shape_area = shape_obj.area
        elif type(shape_obj) == Square:
            shape_area = shape_obj.side_length ** 2
        amount_inside = self.area / shape_area
        return int(amount_inside)

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



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

    def set_width(self):
        self.width = side_length
        self.height = side_length

    def set_height(self):
        self.width = side_length
        self.height = side_length

    def set_side(self, new_length):
        self.side_length = new_length

    def set_width(self, new_length):
        self.side_length = new_length

    def set_height(self, new_length):
        self.side_length = new_length

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

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


Your browser information:

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

Challenge Information:

Build a Polygon Area Calculator - Build a Polygon Area Calculator

try this

rect = Rectangle(3, 6)
sq = Square(5)
rect.set_height(10)
rect.set_width(15)
print(rect.get_amount_inside(sq))

figure out why it’s not working

1 Like

Thank you! I added lines to the setters for height and width which re-calculated the area and it passed - is this the best/most intuitive way to do this?

it’s a way, at this point of your learning journey, focus on writing things that work

1 Like