Command Palette

Search for a command to run...

Hectal
Phase 5: Functions
Python · Exercise 2 of 6 PreviousNext
Beginner

Fix the shared basket

The starter function remembers items between calls, because its default basket=[] is created only once. Rewrite it as add_item(item, basket=None) so that each call without a basket starts with a fresh empty list.

It must append the item to the basket and return the list. If the caller passes a list, use and change that list.

What Check tests

  1. add_item("apple") returns ['apple']
  2. A second call starts fresh
  3. A list passed in is used
  4. An empty list passed in is still used
  5. The default is None

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.