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 trialEmil Rabinovich
2,638 PointsSortedInventory: 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
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()
6 Answers
Krishna Pratap Chouhan
15,203 Pointsi 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();
George Nhari
12,136 Pointssomeone like me
Tianyang Li
3,308 PointsAll 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
Simla Sophia Yildirim
12,949 PointsHi 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
Python Web Development Techdegree Graduate 18,701 PointsSimla 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.
Simla Sophia Yildirim
12,949 PointsThank you! Khem Myrick
Anthony Dibaya
10,194 PointsThese dont work
Anthony Dibaya
10,194 Pointsnvm they do
Mike Siwik
Full Stack JavaScript Techdegree Student 8,483 Pointscommenting out super().add_item(item) passes the test... Took me a while to figure out, but passing the tets making it worth it.
wads wdsf
Python Web Development Techdegree Student 1,678 Pointsclass SortedInventory(Inventory):
def add_item(self,item):
Inventory.add_item(self,item)
self.slots.sort()
J llama
12,631 Pointswrong. thanks for wasting my time. why would you post something in here that is not correct and not even leave a comment?
Akbar Tahir
Courses Plus Student 2,060 PointsAkbar Tahir
Courses Plus Student 2,060 Pointsclass SortedInventory(Inventory): def init(self,slots=[]): self.slots = slots
worked !!