Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Inheritance Super!

iam getting this notification for this part of the task: Bummer! Hmm, the items don't seem to be sorted

plz tell me what's going wrong with a code to solve this because i need it

inventory.py
class Inventory:
    def __init__(self):
        self.slots = []

    def add_item(self, item):
        self.slots.append(item)
        self.slots.sort()


class SortedInventory(Inventory):

    def add_item(self, item):
        super().add_item(item)
        self.slots.append(item)
        self.slots.sort()

2 Answers

Steven Parker
Steven Parker
229,732 Points

In your override, you call the "super" version of "add_item" to do add the item to the list. But then you also do an "append", which will cause a duplicate to be added.

Also, the instructions say "Only do this in the SortedInventory class.", but it looks like you also modified the "Inventory" class to do sorting.

Ah, this answer was helpful for me -- I was adding duplicate items too! Thanks Steven :)