Build a Polygon Area Calculator - Build a Polygon Area Calculator

Tell us what’s happening:

I’m unclear as to what the methods for class Square, including set_width, set_height and set_side should be doing. After reading the instructions, it seems to me that they should all be doing roughly the same thing, but I know that should not be the case.

Your code so far

class Rectangle:
  def __init__(self, width, height):
    self.__width = width 
    self.__height = height

  #get_width property
  @property
  def width(self):
    return self.__width

  #set_width property
  def set_width(self, new_width):
    self.__width = new_width
    return self.__width
  
  #get_height property
  @property
  def height(self):
    return self.__height

  #set_height property
  def set_height(self, new_height):
    self.__height = new_height
    return self.__height

  # get_area method
  def get_area(self):
    area = self.__width * self.__height
    return area

  #get_perimeter method
  def get_perimeter(self):
    perimeter = 2 * (self.__width + self.__height)
    return perimeter

  #get_diagonal(length) method
  def get_diagonal(self):
    diagonal = (self.__width ** 2 + self.__height ** 2) ** 0.5
    return diagonal

  #get_picture method
  def get_picture(self):
    if self.__width > 50 or self.__height > 50:
      return "Too big for picture."
    
    else:
      # Variable to contain image
      picture = ""
      
      # Every unit of height, is a row of * reflecting the width
      for row_star in range(self.height):
        picture += "*" * self.__width + "\n"
      return picture
 
  def get_amount_inside(self, shape):

    if isinstance(shape, (Rectangle, Square)):
      Base_area = self.get_area()
      Input_area = shape.get_area()
      return Base_area // Input_area

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

class Square(Rectangle):
  def __init__(self, side_length):
    # super() initialises the width and height parameters of rectnagle as side_length and side_length
    super().__init__(side_length, side_length)  
    self.__side_length = side_length

  def set_width(self, new_width):
    self.__width = new_width
    self.__height = new_width

  def set_height(self, new_height):
    self.__height = new_height
    self.__width = new_height

  def set_side(self):
    self.__height = self.__side_length
    self.__height = self.__side_length

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Challenge Information:

Build a Polygon Area Calculator - Build a Polygon Area Calculator

why not? what are the width and height of a square?

They should be equal to the side_length when the given square object is initialised, i.e. they are equal to one another and side_length.

These two lines seem to be the same

You’re right, sorry about that. After correcting that, I’m only missing steps 15, 16, and 18.

Starting with step 15, with my current lines of code, this is the output:
”None
Square(side=10)
None
Square(side=5)”

Does this not fit the new string representation or have I misunderstood a given aspect of it?

  1. An instance of the Square class should have a different string representation after setting new values by using .set_side().

How did you test this? I don’t see the code you used to generated the output your provided?

Sorry about that, I had on my google colab version.

”square_1 = Square(10)
print(square_1.set_side())
print(square_1)

square_1 = Square(5)
print(square_1.set_side())
print(square_1)”

Why are you printing this? It has no output so you can just call it.

What does set_side do?

Have a look at the Usage Example in the intsructions.

sq = Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)
print(sq.get_picture())
class Rectangle:
  def __init__(self, width, height):
    self.__width = width 
    self.__height = height

  #get_width property
  @property
  def width(self):
    return self.__width

  #set_width property
  def set_width(self, new_width):
    self.__width = new_width
    return self.__width
  
  #get_height property
  @property
  def height(self):
    return self.__height

  #set_height property
  def set_height(self, new_height):
    self.__height = new_height
    return self.__height

  # get_area method
  def get_area(self):
    area = self.__width * self.__height
    return area

  #get_perimeter method
  def get_perimeter(self):
    perimeter = 2 * (self.__width + self.__height)
    return perimeter

  #get_diagonal(length) method
  def get_diagonal(self):
    diagonal = (self.__width ** 2 + self.__height ** 2) ** 0.5
    return diagonal

  #get_picture method
  def get_picture(self):
    if self.__width > 50 or self.__height > 50:
      return "Too big for picture."
    
    else:
      # Variable to contain image
      picture = ""
      
      # Every unit of height, is a row of * reflecting the width
      for row_star in range(self.height):
        picture += "*" * self.__width + "\n"
      return picture

    #get_amount_inside method
    # (Question) Don't know how to avoid problem of only considering area and not shape, i.e. empty space, simply squeezing and morphing the input shape

  def get_amount_inside(self, shape):

    if isinstance(shape, (Rectangle, Square)):
      Base_area = self.get_area()
      Input_area = shape.get_area()
      return Base_area // Input_area

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

class Square(Rectangle):
  def __init__(self, side_length):
    # super() initialises the width and height parameters of rectnagle as side_length and side_length
    super().__init__(side_length, side_length)  
    self.__side_length = side_length

  def set_width(self, new_width):
    self.__side_length = new_width
    self.__width = new_width
    self.__height = new_width

  def set_height(self, new_height):
    self.__side_length = new_height
    self.__height = new_height
    self.__width = new_height

  def set_side(self, new_side_length):
    self.__side_length = new_side_length
    self.__height = new_side_length
    self.__width = new_side_length

  def get_area(self):
    area = self.__side_length * self.__side_length
    return area

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

sq = Square(9)
print(sq)
print(sq.get_area())
print(sq.get_picture())

sq.set_side(4)
print(sq)
print(sq.get_area())
print(sq.get_picture())



This is my code as of now. What I don’t understand is why the value of self.__side_length does not seem to be consistent. After having set the square side_length to be 4 and calling “print(sq)”, it outputs “Square(side=4)”, meaning self.__side_length should have a value of 4, but that does seem to be the case when I call the get_area method.

From my understanding, given that I have defined another operation under the method name get_area, it should override the one it inherited from the class Rectangle, any given that the self.__side_length value did update, I don’t understand why it would not also return an updated area.

I’m a bit puzzled by that.

Just to be clear, this is the problem:

sq = Square(9)
print(sq)
print(sq.get_picture())
sq.set_side(4)
print(sq)
print(sq.get_picture())
Square(side=9)
*********
*********
*********
*********
*********
*********
*********
*********
*********

Square(side=4)
*********
*********
*********
*********
*********
*********
*********
*********
*********

I guess you could write another get_picture() in the square class, but it kind of defeats the purpose

notice how here you are using self.height, when does that get a value?

I met all the conditions, though I don’t think I did so as intended.

set_width and set_height should simple call set_side. I just did this too. i was confused too, but the logic is very simple.

Also,

def set_side(self):
    self.__height = self.__side_length
    self.__height = self.__side_length

You declared height twice.