This project puts the whole module together. The shop wants a restock report: every product the supplier has fewer than 10 of, with its stock and its price, sorted by name. No single answer has all of that. /prices has the names and prices, and /stock has the counts, but only if you send the X-Api-Key header.
Joining two answers: both lists use the same id for a product, and that is the only thing that links them. Turn the stock list into a dictionary that maps each id to its count. Then stock[product["id"]] gives any product's stock in one step, with no searching.
Sorting dictionaries: sort() can't compare two dictionaries on its own. Give it key=: a function that takes one item and returns the value to sort by. For example, to sort a cart by price:
def by_price(item):
return item["price"]
cart.sort(key=by_price)
Pass the function without parentheses: sort calls it on every item for you. Your script must print exactly these lines:
Lamp: 8 left, price 14
Notebook: 0 left, price 3
Plant: 0 left, price 6
Your task: SUPPLIER and API_KEY are given. Read /prices, read /stock with the key, build the dictionary stock from id to count, then print every product with fewer than 10 left, sorted by name: its name with a colon, its stock, left, price and its price.
Assignment
Follow the Coding Tutorial to build the restock report: two requests, a dictionary of stock by id, the printing loop and a sort by name.
Hint
Build stock before the loop, starting from stock = {}. In the loop, compare stock[product["id"]] with 10 using <. Call prices.sort(key=by_name) just before the loop, with no parentheses after by_name.
Free plan: 30 tutor messages. PRO is unlimited.
