Hi, I keep failing test #16, and actually, I realise that one of my problems is that if I set a new height or a new width for a square, only one side changes, and then both sides are not equal, which is, I suppose, the reason why I fail. But I am not sure how I can make sure that if I change any side of square (height or width), all sides get automatically changed and equal. Can you please put me on the right track?
Your code so far
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = 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 + 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."
else:
picture = ''
for i in range(0, self.height):
picture += "*" * self.width + '\n'
return picture
def __str__(self):
return f'Rectangle(width={self.width}, height={self.height})'
def get_amount_inside(self, shape):
times = 0
if (shape.width < self.width) and (shape.height < self.height):
times = (self.height // shape.height) * (self.width // shape.width)
return times
else:
times = 0
return times
class Square(Rectangle):
def __init__(self, side):
Rectangle.width = side
Rectangle.height = side
def set_side(self,side):
Rectangle.width = side
Rectangle.height = side
def set_width(self, side):
Rectangle.width = side
def set_height(self, side):
Rectangle.height = side
def __str__(self):
return f'Square(side={Rectangle.width})'
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())
sq.set_width(6)
print(sq)
print(sq.get_picture())
rect.set_height(8)
rect.set_width(16)
print(rect.get_amount_inside(sq))
sq = Square(5)
print(sq)
sq.set_height(2)
print(sq)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3.1 Safari/605.1.15
Challenge Information:
Build a Polygon Area Calculator - Build a Polygon Area Calculator
Actually, I thought I should replace Rectangle by Square, but then I not only fail test #16 but also #18. I am lost. I have the impression it is simple, but I do not see the solution.
No, it does not give me the right answer for set_height(5), and that is what I do not understand. because it does give me the right answer for set-width(4).
What I do not understand is that, using the same syntax for set_width and for set_height in the Square section, I get the right output for set_width (Square(side = number put as test) but I get the wrong output for set_height. So, the new set_width overrides the previous set_width method, but it is not the case for set_height. Can you please share explanations as to why?
When a Square object is created, it should be initialized with a single side length. The __init__ method should store the side length in both the width and height