Python Forum
Project 4 Shape Calculator FreeCodeCamp
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Project 4 Shape Calculator FreeCodeCamp
#1
Hey All,

I have basically completed the code for this assignment, but I have receiving 3 errors and 3 failures. Anyways been trying to figure them out but to no prevail. Any help would be wicked.
My Code
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:
            pwidth = "*" * self.width
            for n in range(0, self.height):
                line = pwidth + "\n" + line
            return line.rstrip()
        elif self.height <= 50:
            pwidth = "*" * self.width
            for n in range(0, self.height):
                line = pwidth + "\n" + line
            return line.rstrip()
        else:
            return "\"Too big for picture.\""
    def get_amount_inside(self, shape):
        self.shape = shape
        if self.shape == shape:
            num_times = self.get_area()/(sq.width * sq.width)
            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
test code
import unittest
import shape_calculator


class UnitTests(unittest.TestCase):
    def setUp(self):
        self.rect = shape_calculator.Rectangle(3, 6)
        self.sq = shape_calculator.Square(5)

    def test_subclass(self):
        actual = issubclass(shape_calculator.Square, shape_calculator.Rectangle)
        expected = True
        self.assertEqual(actual, expected, 'Expected Square class to be a subclass of the Rectangle class.')

    def test_distinct_classes(self):
        actual = shape_calculator.Square is not shape_calculator.Rectangle
        expected = True
        self.assertEqual(actual, expected, 'Expected Square class to be a distinct class from the Rectangle class.')

    def test_square_is_square_and_rectangle(self):
        actual = isinstance(self.sq, shape_calculator.Square) and isinstance(self.sq, shape_calculator.Square)
        expected = True
        self.assertEqual(actual, expected, 'Expected square object to be an instance of the Square class and the Rectangle class.')

    def test_rectangle_string(self):
        actual = str(self.rect)
        expected = "Rectangle(width=3, height=6)"
        self.assertEqual(actual, expected, 'Expected string representation of rectangle to be "Rectangle(width=3, height=6)"')

    def test_square_string(self):
        actual = str(self.sq)
        expected = "Square(side=5)"
        self.assertEqual(actual, expected, 'Expected string representation of square to be "Square(side=5)"')

    def test_area(self):
        actual = self.rect.get_area()
        expected = 18
        self.assertEqual(actual, expected, 'Expected area of rectangle to be 18')
        actual = self.sq.get_area()
        expected = 25
        self.assertEqual(actual, expected, 'Expected area of rectangle to be 25')
        

    def test_perimeter(self):
        actual = self.rect.get_perimeter()
        expected = 18
        self.assertEqual(actual, expected, 'Expected perimeter of rectangle to be 18')
        actual = self.sq.get_perimeter()
        expected = 20
        self.assertEqual(actual, expected, 'Expected perimeter of rectangle to be 20')

    def test_diagonal(self):
        actual = self.rect.get_diagonal()
        expected = 6.708203932499369
        self.assertEqual(actual, expected, 'Expected diagonal of rectangle to be 6.708203932499369')
        actual = self.sq.get_diagonal()
        expected = 7.0710678118654755
        self.assertEqual(actual, expected, 'Expected diagonal of rectangle to be 7.0710678118654755')

    def test_set_atributes(self):
        self.rect.set_width(7)
        self.rect.set_height(8)
        self.sq.set_side(2)
        actual = str(self.rect)
        expected = "Rectangle(width=7, height=8)"
        self.assertEqual(actual, expected, 'Expected string representation of rectangle after setting new values to be "Rectangle(width=7, height=8)"')
        actual = str(self.sq)
        expected = "Square(side=2)"
        self.assertEqual(actual, expected, 'Expected string representation of square after setting new values to be "Square(side=2)"')
        self.sq.set_width(4)
        actual = str(self.sq)
        expected = "Square(side=4)"
        self.assertEqual(actual, expected, 'Expected string representation of square after setting width to be "Square(side=4)"')

    def test_rectangle_picture(self):
        self.rect.set_width(7)
        self.rect.set_height(3)
        actual = self.rect.get_picture()
        expected = "*******\n*******\n*******\n"
        self.assertEqual(actual, expected, 'Expected rectangle picture to be different.')     

    def test_squaree_picture(self):
        self.sq.set_side(2)
        actual = self.sq.get_picture()
        expected = "**\n**\n"
        self.assertEqual(actual, expected, 'Expected square picture to be different.')   

    def test_big_picture(self):
        self.rect.set_width(51)
        self.rect.set_height(3)
        actual = self.rect.get_picture()
        expected = "Too big for picture."
        self.assertEqual(actual, expected, 'Expected message: "Too big for picture."')

    def test_get_amount_inside(self):
        self.rect.set_height(10)
        self.rect.set_width(15)
        actual = self.rect.get_amount_inside(self.sq)
        expected = 6
        self.assertEqual(actual, expected, 'Expected `get_amount_inside` to return 6.')

    def test_get_amount_inside_two_rectangles(self):
        rect2 = shape_calculator.Rectangle(4, 8)
        actual = rect2.get_amount_inside(self.rect)
        expected = 1
        self.assertEqual(actual, expected, 'Expected `get_amount_inside` to return 1.')

    def test_get_amount_inside_none(self):
        rect2 = shape_calculator.Rectangle(2, 3)
        actual = rect2.get_amount_inside(self.rect)
        expected = 0
        self.assertEqual(actual, expected, 'Expected `get_amount_inside` to return 0.')
        
if __name__ == "__main__":
    unittest.main()
Errors
Error:
.F..EEE.F....F. ====================================================================== ERROR: test_get_amount_inside (test_module.UnitTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/runner/LightAcclaimedLogin/test_module.py", line 98, in test_get_amount_inside actual = self.rect.get_amount_inside(self.sq) File "/home/runner/LightAcclaimedLogin/shape_calculator.py", line 39, in get_amount_inside num_times = self.get_area()/(sq.width * sq.width) NameError: name 'sq' is not defined ====================================================================== ERROR: test_get_amount_inside_none (test_module.UnitTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/runner/LightAcclaimedLogin/test_module.py", line 110, in test_get_amount_inside_none actual = rect2.get_amount_inside(self.rect) File "/home/runner/LightAcclaimedLogin/shape_calculator.py", line 39, in get_amount_inside num_times = self.get_area()/(sq.width * sq.width) NameError: name 'sq' is not defined ====================================================================== ERROR: test_get_amount_inside_two_rectangles (test_module.UnitTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/runner/LightAcclaimedLogin/test_module.py", line 104, in test_get_amount_inside_two_rectangles actual = rect2.get_amount_inside(self.rect) File "/home/runner/LightAcclaimedLogin/shape_calculator.py", line 39, in get_amount_inside num_times = self.get_area()/(sq.width * sq.width) NameError: name 'sq' is not defined ====================================================================== FAIL: test_big_picture (test_module.UnitTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/runner/LightAcclaimedLogin/test_module.py", line 93, in test_big_picture self.assertEqual(actual, expected, 'Expected message: "Too big for picture."') AssertionError: '*****************************************[112 chars]****' != 'Too big for picture.' + Too big for picture.- *************************************************** - *************************************************** - *************************************************** : Expected message: "Too big for picture." ====================================================================== FAIL: test_rectangle_picture (test_module.UnitTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/runner/LightAcclaimedLogin/test_module.py", line 80, in test_rectangle_picture self.assertEqual(actual, expected, 'Expected rectangle picture to be different.') AssertionError: '*******\n*******\n*******' != '*******\n*******\n*******\n' ******* ******* - *******+ ******* ? + : Expected rectangle picture to be different. ====================================================================== FAIL: test_squaree_picture (test_module.UnitTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/runner/LightAcclaimedLogin/test_module.py", line 86, in test_squaree_picture self.assertEqual(actual, expected, 'Expected square picture to be different.') AssertionError: '**\n**' != '**\n**\n' ** - **+ ** ? + : Expected square picture to be different. ---------------------------------------------------------------------- Ran 15 tests in 0.004s FAILED (failures=3, errors=3) 
Reply
#2
At lest, it tells you that sq is undefined. Take a look at line No. 39 (first code snippet), you probably need to use self.width and self.height, instead of sq.width and sq.height.
Reply
#3
Hey thanks, I'll test it out. I just debugged the other 3 failures. So now I only have the 3 errors.
Reply
#4
Yeah unfortunately that didn't work. When I use the self methods the math isn't adding up, but when i refer to the instances the math works but the errors come back about the referencing.
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
Error:
50 26 Rectangle(width=3, height=10) 81 5.656854249492381 Square(side=4) ....FF......... ====================================================================== FAIL: test_get_amount_inside (test_module.UnitTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/runner/SympatheticImpracticalParallelprocessing/test_module.py", line 100, in test_get_amount_inside self.assertEqual(actual, expected, 'Expected `get_amount_inside` to return 6.') AssertionError: 1 != 6 : Expected `get_amount_inside` to return 6. ====================================================================== FAIL: test_get_amount_inside_none (test_module.UnitTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/runner/SympatheticImpracticalParallelprocessing/test_module.py", line 112, in test_get_amount_inside_none self.assertEqual(actual, expected, 'Expected `get_amount_inside` to return 0.') AssertionError: 1 != 0 : Expected `get_amount_inside` to return 0. ---------------------------------------------------------------------- Ran 15 tests in 0.026s FAILED (failures=2)
Reply
#5
Take a look at implementation of theget_amount_inside function. You have condition that does not work. Code blocks in after if and after else statements are the same. Moreover, you compute area and divide it to production of width and height (i.e. area too). This is why you got 1. I don't understand what get_amount_inside means. amount of what?
Reply
#6
The get_amount_inside formula is supposed to be the rectangular area divided by the area of the shape that is entered in the get_amount_inside condition and compute 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

Hey thanks for your help i figured it out.

it needed to be shape.width * shape.height
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Graphics Help - Filling each shape in Nate 10 7,199 Nov-27-2018, 08:40 AM
Last Post: Nate
  ValueError: shape mismatch: value array of shape... ulrich48155 2 23,681 Jul-10-2017, 02:17 PM
Last Post: ulrich48155

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020