Checkpoint

Checkpoint: The Case of the Wrong Total

The Guild shop's till has gone haywire. Shopkeeper Mo types each sale as name,quantity,price (prices in whole coins). The receipt below should total 2*5 + 1*12 + 3*4 = 34 coins. Instead the till crashes:

Traceback (most recent call last):
  File "main.py", line 15, in <module>
    print(receipt_total(receipt))
  File "main.py", line 10, in receipt_total
    name, qty, price = parse_line(line)
  File "main.py", line 2, in parse_line
    name, qty, price = line.split(",")
ValueError: not enough values to unpack (expected 3, got 1)

Use the whole chapter: read the clue, print the evidence, set a tripwire.

Assignment

  1. Fix the crash: receipt_total(lines) must skip blank lines, including ones that are only spaces.
  2. Once it stops crashing, the total is still wrong. Find out why (print the evidence!) and fix it, so every line is added up.
  3. Add a tripwire in parse_line: assert qty > 0 with a message. parse_line("rope,0,4") must raise AssertionError.