Bug hunt

Print the Evidence

The worst bugs don't crash. The program runs and prints a wrong answer, and there is no traceback to read. Time to gather evidence!

Add a print right where the value is made, so you can watch it change. f-strings have a detective shortcut: put = after the name.

weight = 3
item = "rope:3"
print(f"{item=} {weight=}")   # item='rope:3' weight=3
print(f"{weight * 2=}")       # weight * 2=6

{x=} prints the name and the value. Text values get quotes, so hidden spaces show up: name=' Pip'.

Good evidence prints are targeted: one or two values, inside the loop or function where things go wrong. Twenty prints everywhere just bury the clue.

The bug report

total_weight(items) should add up the weights in a list like ["rope:3", "lamp:2", "anchor:12"]. That pack weighs 17, but the function says 7.

Assignment

  1. Inside the loop of total_weight, add a debug print that uses the = shortcut, such as print(f"{item=} {weight=}"). Run it and find the item whose weight is read wrong.
  2. Fix how weight is read so it works for any whole number, like "cannon:150" or "feather:0". (Hint: split the text at ":".)
  3. Keep your debug print in for this lesson, so Pip can see your evidence.