Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assertion Error
#1
Hello everyone,
I've got problem with assertion. It seems that it should work, but after running the program I receive AssertionError. Do you have any idea why and how I can fix it/modify declaration of classes? Think
This is my code:
class Figure1:
    def __init__(self, x, y, z):
        self._x = x
        self._y = y
        self._z = z
    @property
    def area(self):
        return (self._x + self._y) * self._z / 2
class Figure2(Figure1):
    def __init__(self, x, z):
        super().__init__(x=x, y=x, z=z)
class Figure3(Figure2):
    @property
    def circumference(self):
        return self._x * 4

class Figure4(Figure2):

    def __init__(self, x, y):
        super().__init__(x=x,z=y)
    @property
    def circumference(self):
        return 2 * self._x + 2 * self._z

class Figure5(Figure4, Figure3):

    def __init__(self, x):
        super().__init__(x=x, y=x)

a = Figure5(9).area
print(a)
b = Figure5(9).circumference
print(b)
if __name__ == "__main__":
    Figure5.x = 9
    assert Figure5.circumference == 36
    assert Figure5.area == 81


and the error:
Error:
81.0 Traceback (most recent call last): 36 line 37, in <module> assert Figure5.circumference == 36 AssertionError Process finished with exit code 1
Reply
#2
You should be creating a Figure5 instance: check = Figure5(5). Run your assertions on that. Currently you are testing the class itself.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
"Figure5" is a class, not an instance of a class. As such, there is no "self" to provide to circumference(). If you remove the property annotation from circumference, a second error appears because of the lack of self. You need to instantiate the class and then call circumference to make it work.

On a side note, you may encounter issues with Figure5 because of multiple inheritance because both of its parents have a circumference method.

class Figure1:
    def __init__(self, x, y, z):
        self._x = x
        self._y = y
        self._z = z

    def area(self):
        return (self._x + self._y) * self._z / 2

class Figure2(Figure1):
    def __init__(self, x, z):
        super().__init__(x=x, y=x, z=z)

class Figure3(Figure2):
    
    def circumference(self):
        return self._x * 4

class Figure4(Figure2):

    def __init__(self, x, y):
        super().__init__(x=x,z=y)

    def circumference(self):
        return 2 * self._x + 2 * self._z

class Figure5(Figure4, Figure3):

    def __init__(self, x):
        super().__init__(x=x, y=x)
 
a = Figure5(9)
print(a.area())
print(a.circumference())

if __name__ == "__main__":
    a.x = 9
    assert a.circumference() == 36
    assert a.area() == 81
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  opencv on mac: Assertion Failed RandomCoder 0 1,637 Feb-16-2020, 06:17 PM
Last Post: RandomCoder
  Test Case Assertion Error moga2003 10 5,373 Mar-18-2019, 01:06 AM
Last Post: moga2003
  error: (-215:Assertion failed) gkiller007 1 8,630 Jan-04-2019, 04:27 AM
Last Post: stullis

Forum Jump:

User Panel Messages

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