# Polygon Area Calculator -PYTHON

Tell us what’s happening:
So I am receiving 1 failure and 1 error and I am not sure how to fix this. Any insight would be greatly appreciated

``````class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def __str__(self):
return "Rectangle(width={}, height={})".format(self.width, self.height)
def set_width(self, width):
self.width = width
return self.width
def set_height(self, height):
self.height = height
return self.height
def get_area(self):
area = self.width * self.height
return area
def get_perimeter(self):
perm = (self.width * 2) + (self.height * 2)
return perm
def get_diagonal(self):
diag = (self.width ** 2 + self.height ** 2) ** .5
return diag
def get_picture(self):
line = ""
if self.width > 50 or self.height > 50:
return "Too big for picture."
if self.width <= 50:
pwidth = "*" * self.width
for n in range(0, self.height):
line = pwidth + "\n" + line
return line
elif self.height <= 50:
pwidth = "*" * self.width
for n in range(0, self.height):
line = pwidth + "\n" + line
return line

def get_amount_inside(self, shape):
self.shape = shape
if type(self.shape) == Square:
num_times = self.get_area()/(sq.width * sq.width)
return int(num_times)

else:
num_times = self.get_area()/(self.width * self.height)
return (num_times)

class Square(Rectangle):
def __init__(self, side):
self.width = side
self.height = side
def __str__(self):
return "Square(side={})".format(self.width)
def set_side(self, side):
self.width = side
self.height = side
return self.width, self.height
def set_width(self, side):
self.width = side
self.height = side
return self.width, self.height
def set_height(self, side):
self.height = side
self.width = side
return self.height, self.width
``````

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

Challenge: Polygon Area Calculator

The error says: sq is not defined
num_times = self.get_area()/(sq.width * sq.width)

Do you mean probably: self?
num_times = self.get_area()/(self.width * self.width)

1 Like

Hey when I use “self” the math ends up being wrong although i get no errors. Not sure how to reference the instances in order for the results to be correct. I end up with this:

``````class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def __str__(self):
return "Rectangle(width={}, height={})".format(self.width, self.height)
def set_width(self, width):
self.width = width
return self.width
def set_height(self, height):
self.height = height
return self.height
def get_area(self):
area = self.width * self.height
return area
def get_perimeter(self):
perm = (self.width * 2) + (self.height * 2)
return perm
def get_diagonal(self):
diag = (self.width ** 2 + self.height ** 2) ** .5
return diag
def get_picture(self):
line = ""
if self.width > 50 or self.height > 50:
return "Too big for picture."
if self.width <= 50:
pwidth = "*" * self.width
for n in range(0, self.height):
line = pwidth + "\n" + line
return line
elif self.height <= 50:
pwidth = "*" * self.width
for n in range(0, self.height):
line = pwidth + "\n" + line
return line

def get_amount_inside(self, shape):
self.shape = shape
if type(self.shape) == Square:
num_times = self.get_area()/(self.width * self.height)
return int(num_times)
else:
num_times = self.get_area()/(self.width * self.height)
return int(num_times)

class Square(Rectangle):
def __init__(self, side):
self.width = side
self.height = side
def __str__(self):
return "Square(side={})".format(self.width)
def set_side(self, side):
self.width = side
self.height = side
return self.width, self.height
def set_width(self, side):
self.width = side
self.height = side
return self.width, self.height
def set_height(self, side):
self.height = side
self.width = side
return self.height, self.width
``````

That means that you are using the wrong formula if you don’t get the value you expected. Self ‘references the instance’. Your formula is `area/area`.

1 Like

the formula works when I use sq.width and sq.height. I only get those errors when i switch to self.

The formula is supposed to be the rectangular area divided by the area of the shape that is entered in the get_amount_inside condition for the number of times the shape can fit in. Example Rec Area width= 16 x length= 8x = 128 and Sq area = 4x4 = 16
Number of times sq fit in rec area = 8

Ah. Then you want to use the height and width of the `shape`. Though, ideally your `square` would have a `get_area` method.

1 Like

Thank you! it worked and I now have zero failures or errors. Not sure why that wasn’t clicking in my brain before lol. Cheers!

1 Like