Python · Exercise 1 of 6Next
Intermediate
Fix the list that remembers too much
The function add_tag(tag, tags=[]) appends tag to a list and returns the list. A call that does not pass tags should start from a new, empty list, but today every such call shares one list. (See Topic 17.1.)
Fix it. If the caller passes a list, append to that same list and return it (the same object). Do not use [] as a default value.
What Check tests
- Two calls without a list give independent results
- A list passed in is extended and returned (same object)
- An empty list passed in is used, not replaced
- The default is not a list
- The program prints ['a'] then ['b']
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.