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!

Emil Rabinovich
Emil Rabinovich
2,638 Points

SortedInventory: super() - issue with task

Hi Guys,

I'm keep getting "Bummer! Hmm, the items don't seem to be sorted" on below task:

"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."

Well, I'm pretty sure it is sorted, what am I missing?

Thanks, Emil

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

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


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

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

class SortedInventory(Inventory): def init(self,slots=[]): self.slots = slots

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

worked !!

6 Answers

i know its late, if someone comes here,

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();

someone like me

All the answers posted may pass 'technically', but it doesn't actually answer the task at hand.

all you need to simply do is call self.slot.sort(). This is using the list inherited by the parent Inventory and just simply adding the function list.sort().

And since you're not calling the super() function here the method stays within the class (here it is class SortedInventory)

The following will suffice:

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

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

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

Hi thereˆ I exactly wrote the same code except "pass" keyword down there in your code, but mine didn't work. Can you explain me what that keyword stands for in this case? Tianyang Li

Khem Myrick
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Khem Myrick
Python Web Development Techdegree Graduate 18,701 Points

Simla Sophia Yildirim, I just did this exercise, using code that looked like what Tianyang wrote (without the "pass" keyword), I was able to proceed. If your code looks mostly like what's above, but isn't passing, you may just want to check for typos.

Thank you! Khem Myrick

These dont work

nvm they do

Mike Siwik
seal-mask
.a{fill-rule:evenodd;}techdegree
Mike Siwik
Full Stack JavaScript Techdegree Student 8,483 Points

commenting out super().add_item(item) passes the test... Took me a while to figure out, but passing the tets making it worth it.

class SortedInventory(Inventory):
  def add_item(self,item):
  Inventory.add_item(self,item)
  self.slots.sort()
J llama
J llama
12,631 Points

wrong. thanks for wasting my time. why would you post something in here that is not correct and not even leave a comment?