Gazza732 Gazza732 - 8 months ago 62
Python Question

Why am I getting an Assertion Error on this Python code?

I have a function written here:

def addItem(aBookcase, name, mediaType):
Returns False if aBookcase is full, otherwise returns True and
adds item of given name and mediaType to aBookcase.
emptySpacesBefore = aBookcase.getEmptySpaces()
if aBookcase.getEmptySpaces() == 0:
added = False
return added
position = findSpace(aBookcase)
aBookcase.setName(*position, name=name)
aBookcase.setType(*position, mediaType=mediaType)
added = True
emptySpacesAfter = aBookcase.getEmptySpaces()
assert added is True, "No free positions"
assert emptySpacesAfter < emptySpacesBefore, "Same amount of empty spaces"
assert aBookcase.getName(*position) is name, "Error with name"
assert aBookcase.getType(*position) is mediaType, "Error with media type"

Yet when I go to test the function with this line of code:

assert addItem(small, "Algorhythms, date structures and compatibility", BOOK)

I get an 'AssertionError' as shown here:

Screenshot of 'AssertionError'

So if I'm right, it means I'm not handling it but I'm not sure how or why? Is it something wrong with my code? Something missing? etc.

Answer Source

when it works properly, your addItem function returns nothing, so it returns None, which is seen as a failure by the last assert statement that you inserted. You should return added for both cases (True or False)

BTW since you reached that line, it means that all previous assertions are OK so good news: your code is OK.