Skip to the content

Project: sales report

Python · Unit 16: The standard library

The day's sales arrive as JSON text: a list of dictionaries, each one with the item and how many units were sold.

With json.loads you turn them into data and with Counter you sum them up. Let's build the report in two steps.

import json

t = '[{"item": "jam", "qty": 2}]'
sales = json.loads(t)
print(sales[0]["item"])

Prints

jam

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Complete the code

    Complete the count. It should print: {'jam': 3}

  2. 2. Write the code

    Write units(text): it takes the sales JSON and returns a dictionary with how many units of each item were sold. With [] it returns {}.

  3. 3. Write the code

    Write best_seller(text): it returns the name of the item with the most units sold in that JSON, or "-" if there were no sales.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →