One-Line Receipt

Python for AIMedium+10 XP+10

One-Line Receipt

A shop saves a one-line summary of each basket.

prices is a dictionary of item name to price in whole pounds, like {"tea": 3, "jam": 4}.

Write save_summary(path, prices). It writes one line of text into the file called path, closes it, and returns path. The text is:

1<count> items, total <total>
  • <count> is how many items are in the dictionary.
  • <total> is all the prices added up.
  • If there is exactly one item, write item instead of items.
  • An empty basket writes 0 items, total 0.
  • Don't add a line break at the end.

Keep the peek helper as it is.

Examples

path = save_summary('basket.txt', {'bread': 2, 'milk': 1})
returns '2 items, total 3'
path = save_summary("sum_a.txt", {"tea": 3, "jam": 4, "eggs": 2})
returns '3 items, total 9'
path = save_summary("sum_b.txt", {"apple": 1})
returns '1 item, total 1'

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run