Fix the bug: the second cart is not empty
A small store's checkout script. add_item puts one item in a cart, and if you
do not hand it a cart it starts a fresh one. Ada shops first and gets apples.
Ben shops next and gets bread. Run it and read Ben's cart: Ada's apples are in
it.
Nothing raised. Both calls returned a list, which is exactly what they promised
to return. The only thing wrong is the value - and the value is what reaches the
customer, on the receipt and in the total. A traceback would have stopped the
deploy. This ships.
The data is not doing anything clever. Two shoppers, one ordinary string each,
no None, no empty input, nothing to trip over. The first call is perfect,
which is exactly why nobody catches this in review. What is adversarial here is
the second call . Ada and Ben share nothing in this program except add_item
itself, so whatever is leaking between them is living on the function.
Your task: fix add_item so a call that passes no cart starts from an empty
one, while a call that does pass a cart still adds to that cart. Ben's cart
should be ['bread'].
You'll practice:
Reading a clean run that returns a wrong value as a bug, not a quirk
Tracing leaked state back to when a default argument is actually created
Show a hint
Show solution
Previous Next