Bug hunt

The Backup That Wasn't

In Python, a variable is a name tag stuck onto a value. Writing b = a with a list puts a second tag on the same list. This is called aliasing.

To get a real, separate copy, ask for one:

copy1 = original.copy()   # a new list with the same items
copy2 = original[:]       # slicing everything also copies

Now changing original leaves the copies alone.

Pip tip: Numbers, strings and tuples can't change, so aliasing never bites you with them. Watch out with lists, dictionaries and sets.

The bug

The quartermaster saves a backup of the inventory before a trip. After the trip, the "backup" shows the new items instead of the old ones:

Now: ['rope', 'map', 'shell']
Backup: ['rope', 'map', 'shell']

Assignment

Fix the program so backup keeps the original ['potion', 'rope', 'map'] while inventory changes.