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!

Gabe Dumbrille
Gabe Dumbrille
2,419 Points

Object Oriented Python Super() Challenge 3

No idea how to do this

Sorted inventories should be just that: sorted. Right now, we just add an item onto the slots list whenever our add_item method is called. Use the list.sort() method to make sure the slots list gets sorted after an item is added. Only do this in the SortedInventory class.

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

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

class SortedInventory(Inventory):
    def add_item(self, item):
        self.slots.append(item)

4 Answers

Steven Parker
Steven Parker
229,644 Points

The instructions say "Use the list.sort() method ... after an item is added." And since the "list" in this case is "self.slots", the additional code line would be:

        self.slots.sort()

But also, as mentioned in answer to your other question,, the method for adding the item used here differs from the instructions.

Gabe Dumbrille
Gabe Dumbrille
2,419 Points

Hi Steven,

I posted that question to the community since I couldn't solve it either and I don't know why it worked not using super().

That said, I used "self.sort.slots()" and it just keeps saying "Bummer: Hmm, the items don't seem to be sorted"

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

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

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

This challenge doesn't seem to be working...

Steven Parker
Steven Parker
229,644 Points

The item should only be added once (this code adds it twice, using two different methods).

Gabe Dumbrille
Gabe Dumbrille
2,419 Points

Thanks!

I figured out the issue...when adding super() I had added this also "self.slots.append(item)":

class SortedInventory(Inventory):
    def add_item(self, item):
        self.slots.append(item)
        super().add_item(item)

The "self.slots.append(item)" portion allowed it to work in Task 2, but fail in Task 3.

So apparently it should have been this (below) in Task 2 of the Challenge:

class SortedInventory(Inventory):
    def add_item(self, item):
        super().add_item(item)
Steven Parker
Steven Parker
229,644 Points

Right, as mentioned in answer to your other question.

Glad to help, and happy coding!

Treehouse lacks in through explanations. Mixes subject matters up while explaining them, and fails to analyze aspects thoroughly before moving onto the next subject.