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 trialGeorge Clement
2,816 Pointsconfused with the statement "Use the list.sort() method " is that just creating a sort method in SortedInventory?
I am confused by the statment
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.
I don't know they want a method named sorted on the subclass or something else
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)
1 Answer
Michael Hulet
47,913 PointsA normal Inventory
is not sorted. However, a SortedInventory
is. In order to guarantee this, you need to re-sort the list each time a new item is added. A Python list
already has a built-in method called sort
, which sorts the list. For example:
numbers = [3, 5, 2, 4, 1]
numbers.sort()
print(numbers) # prints [1, 2, 3, 4, 5]
All you need to do is call this method on slots
to sort it after you add an item to it