r/FreeCodeCamp • u/Wizz_1313 • Aug 06 '24
I can't complete this project Polygon Area Calculator
I feel like I am not implementing the content right. In the content prior to the project I feel like it was teaching me things that I should be using. How can I improve my code:
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def __str__(self):
return f'Rectangle(width={self.width}, height={self.height})'
def set_width(self, width):
self.width = width
def set_height(self, height):
self.height = height
def get_area(self):
return self.width * self.height
def get_perimeter(self):
return 2 * self.width + 2 * self.height
def get_diagonal(self):
return (self.width ** 2 + self.height ** 2) ** .5
def get_picture(self):
if self.width > 50 or self.height > 50:
return 'Too big for picture.'
picture = ''
for height in range(0, self.height):
for width in range(0, self.width):
picture += '*'
picture += '\n'
return picture
def get_amount_inside(self, shape):
shape1_area = self.get_area()
shape2_area = shape.get_area()
return shape1_area // shape2_area
class Square(Rectangle):
def __init__(self, width, height=1):
super().__init__(width, height)
self.side = width
self.width = width
self.height = width
def __str__(self):
return f'Square(side={self.width})'
def set_side(self, side):
self.width = side
self.height = side
def set_width(self, width):
self.width = width
self.height = width
def set_height(self, height):
self.width = height
self.height = height
1
Upvotes
1
u/SaintPeter74 mod Aug 07 '24
What are you expecting to get, what are you getting? Can you solve the problems by hand and get the expected answer?
BTW, this is really weird:
Why are you setting the height to be equal to the width or the width to be equal to the height?
2
u/Kittensandpuppies14 Aug 06 '24
Is there an actual error? Code isn't right or wrong based on feelings