Sep-13-2020, 03:08 AM
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
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.widthtest 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)