Polygon calculator, where is my mistake?

hello everyone, I am currently completing the polygon calculator and have encountered a couple of errors that i cannot seem to wrap my head around. On pycharm the code work and does what it is supposed to but on replit the test fail. Which have lead me to think that my mistake lies in the formatting and “general setup” of the code as opposed to it not being able to perform the the task described by the challenge.
Here is my code and below are the 6 tests that is are not passing. I checked the test modules and tried to find the source of my errors but failed. So I am sure that i must be missing something…
Some errors I just straight up not understand, like the first one for example: why does the test_rectangle_picture claim that I return “none” my methods all have a “return” and the code on pycharm works nicely? And what is wrong with the string representation of the shapes?

Thank you all for your help, I am sure that it must be some silly error, but these errors are even more frustrating when you know you are almost finished with a project with the exception of a few quick to fix bugs!

# This entrypoint file to be used in development. Start by reading README.md
import shape_calculator
from unittest import main

class Rectangle:

    @classmethod
    def from_str(cls, str_height, str_width):
        height = int(str_height.lstrip("height ="))
        width = int(str_width.lstrip("width ="))
        cls(width, height)
        return cls(width, height)

    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):
        area = self.width * (self.height)
        return area

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

    def get_diagonal(self):
        diagonal = ((self.width) ** 2 + (self.height) ** 2) ** 0.5
        return diagonal

    def get_picture(self):
        if isinstance(self.width,str) == True or isinstance(self.height, str) == True:
            return"Rectangle(width={}, height={})".format(self.width, self.height)
        elif self.width > 50 or self.height > 50:
           return "Too big for picture."
        else:
            for i in range(int(self.height)):
              x = (self.width) * ("*")
              y = print(x)
              return y

    def get_amount_inside(self, shape_2):
        self.shape2 = shape_2
        x = self.get_area() // shape_2.get_area()
        if x > 0:
            return x
        else:
            return 0


class Square(Rectangle):

    @classmethod
    def from_str(cls, str_side):
        side = int(str_side.lstrip("side ="))
        cls(side, side)
        return cls(side, side)


    def __init__(self, side):
        super().__init__(side, side)

    def set_side(self, side):
        self.side = side


    def get_picture(self):
        if isinstance(self.side,str) == True :
            return"Square(side={})".format(self.side)
        elif self.side > 50:
           return "Too big for picture."
        else:
            for i in range(int(self.side)):
              x = (self.side) * ("*")
              y = print(x)
              return y





#code already included in the challenge:
main(module='test_module', exit=False)

rect = shape_calculator.Rectangle(5, 10)
print(rect.get_area())
rect.set_width(3)
print(rect.get_perimeter())
print(rect)

sq = shape_calculator.Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)


# Run unit tests automatically
main(module='test_module', exit=False)

Here are my the tests I am failing

 python main.py
........*******
FFF.F*****
F.
======================================================================
FAIL: test_rectangle_picture (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-polygon-area-calculator-5/test_module.py", line 80, in test_rectangle_picture
    self.assertEqual(actual, expected, 'Expected rectangle picture to be different.')
AssertionError: None != '*******\n*******\n*******\n' : Expected rectangle picture to be different.

======================================================================
FAIL: test_rectangle_string (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-polygon-area-calculator-5/test_module.py", line 28, in test_rectangle_string
    self.assertEqual(actual, expected, 'Expected string representation of rectangle to be "Rectangle(width=3, height=6)"')
AssertionError: '<shape_calculator.Rectangle object at 0x7f7136baeb80>' != 'Rectangle(width=3, height=6)'
- <shape_calculator.Rectangle object at 0x7f7136baeb80>
+ Rectangle(width=3, height=6)
 : Expected string representation of rectangle to be "Rectangle(width=3, height=6)"

======================================================================
FAIL: test_set_atributes (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-polygon-area-calculator-5/test_module.py", line 66, in test_set_atributes
    self.assertEqual(actual, expected, 'Expected string representation of rectangle after setting new values to be "Rectangle(width=7, height=8)"')
AssertionError: '<shape_calculator.Rectangle object at 0x7f7136bae9a0>' != 'Rectangle(width=7, height=8)'
- <shape_calculator.Rectangle object at 0x7f7136bae9a0>
+ Rectangle(width=7, height=8)
 : Expected string representation of rectangle after setting new values to be "Rectangle(width=7, height=8)"

======================================================================
FAIL: test_square_string (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-polygon-area-calculator-5/test_module.py", line 33, in test_square_string
    self.assertEqual(actual, expected, 'Expected string representation of square to be "Square(side=5)"')
AssertionError: '<shape_calculator.Square object at 0x7f7136baea90>' != 'Square(side=5)'
- <shape_calculator.Square object at 0x7f7136baea90>
+ Square(side=5)
 : Expected string representation of square to be "Square(side=5)"

======================================================================
FAIL: test_squaree_picture (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-polygon-area-calculator-5/test_module.py", line 86, in test_squaree_picture
    self.assertEqual(actual, expected, 'Expected square picture to be different.')
AssertionError: None != '**\n**\n' : Expected square picture to be different.

----------------------------------------------------------------------
Ran 15 tests in 0.002s

FAILED (failures=5)
50
26
<shape_calculator.Rectangle object at 0x7f7137f4c130>
81
12.727922061357855
<shape_calculator.Square object at 0x7f7136baeac0>
........*******
FFF.F*****
F.
======================================================================
FAIL: test_rectangle_picture (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-polygon-area-calculator-5/test_module.py", line 80, in test_rectangle_picture
    self.assertEqual(actual, expected, 'Expected rectangle picture to be different.')
AssertionError: None != '*******\n*******\n*******\n' : Expected rectangle picture to be different.

======================================================================
FAIL: test_rectangle_string (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-polygon-area-calculator-5/test_module.py", line 28, in test_rectangle_string
    self.assertEqual(actual, expected, 'Expected string representation of rectangle to be "Rectangle(width=3, height=6)"')
AssertionError: '<shape_calculator.Rectangle object at 0x7f7136304850>' != 'Rectangle(width=3, height=6)'
- <shape_calculator.Rectangle object at 0x7f7136304850>
+ Rectangle(width=3, height=6)
 : Expected string representation of rectangle to be "Rectangle(width=3, height=6)"

======================================================================
FAIL: test_set_atributes (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-polygon-area-calculator-5/test_module.py", line 66, in test_set_atributes
    self.assertEqual(actual, expected, 'Expected string representation of rectangle after setting new values to be "Rectangle(width=7, height=8)"')
AssertionError: '<shape_calculator.Rectangle object at 0x7f7136304be0>' != 'Rectangle(width=7, height=8)'
- <shape_calculator.Rectangle object at 0x7f7136304be0>
+ Rectangle(width=7, height=8)
 : Expected string representation of rectangle after setting new values to be "Rectangle(width=7, height=8)"

======================================================================
FAIL: test_square_string (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-polygon-area-calculator-5/test_module.py", line 33, in test_square_string
    self.assertEqual(actual, expected, 'Expected string representation of square to be "Square(side=5)"')
AssertionError: '<shape_calculator.Square object at 0x7f7136304c10>' != 'Square(side=5)'
- <shape_calculator.Square object at 0x7f7136304c10>
+ Square(side=5)
 : Expected string representation of square to be "Square(side=5)"

======================================================================
FAIL: test_squaree_picture (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-polygon-area-calculator-5/test_module.py", line 86, in test_squaree_picture
    self.assertEqual(actual, expected, 'Expected square picture to be different.')
AssertionError: None != '**\n**\n' : Expected square picture to be different.

----------------------------------------------------------------------
Ran 15 tests in 0.002s

FAILED (failures=5)
 ^C

Why are you returning y? Are you supposed to print x? Should x have multiple rows?

I don’t see any code setting the string representation.

I am suppose to return a picture of the rectangle drawn by asterisks.
Here is the description of the method:

  • get_picture: Returns a string that represents the shape using lines of “*”. The number of lines should be equal to the height and the number of “*” in each line should be equal to the width. There should be a new line (\n) at the end of each line. If the width or height is larger than 50, this should return the string: “Too big for picture.”.

When i first attempted this my code was:

              x = (self.width) * ("*")
              print(x)
              print("")

but it didn’t seem to like that either, so I assumed that python wanted a return statement, that the tests would independently print.

I must have misinterpreted then what the task says? I assumed that if the information was passed as a string, one must first extract the information (hence the class method serving as an alternative constructor that will automatically parse the strings for me), and later in the “get_picture()” method if the information was given in the form of a string, rather than drawing a picture, it would return the string “Rectangle(width=3, height=6)”’)

Is this right, or have i completely misinterpreted?

Do you have the link to the challenge so I can read the instructions rather than trying to remember them?

https://replit.com/@Beatrixdroid/boilerplate-polygon-area-calculator-6#README.md

here it is!

Ok, so I remember right:

get_picture : Returns a string that represents the shape using lines of “". The number of lines should be equal to the height and the number of "” in each line should be equal to the width. There should be a new line ( \n ) at the end of each line. If the width or height is larger than 50, this should return the string: “Too big for picture.”.

You should not print. You need to construct a multi-line string and return it.


This

Additionally, if an instance of a Rectangle is represented as a string, it should look like: Rectangle(width=5, height=10)

means that you need to define the string representation. I’d Google repr (I think that’s the method)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.