Kodewick
Start learning →
Basic←→0 / 80

Stationery stock

#074BasicClassesDicts5 min

The stationery shop keeps its stock in Stock, where self.items is {item: how many}. Finish add(name, count): if the item is already there, add to it; if not, start from 0.

Examples
stock_after([("pen", 10), ("glue", 2), ("pen", 5)])→{'pen': 15, 'glue': 2}
stock_after([])→{}
main.py
class Stock:
    def __init__(self):
        self.items = {}

    def add(self, name, count):
        # your code here: add count to how many of this item there were
        pass


def stock_after(deliveries):
    shop = Stock()
    for name, count in deliveries:
        shop.add(name, count)
    return shop.items
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.