Code

Collect the Crystal

When Pip picks up a crystal, it should vanish from the map. Otherwise Pip could type take again and again for infinite crystals!

A string can't change, but a list can. So we rebuild one row and store it back into the list:

cave = [".C.", "#.."]
line = cave[0]
cave[0] = line[:1] + "." + line[2:]
print(cave)   # ['...', '#..']

When you pass a list into a function, the function gets the same list, not a copy. So a function can change the map or the backpack, and the change sticks after it returns.

A dict tells us which tiles are items, and their names:

ITEMS = {"C": "crystal", "K": "key", "O": "oil"}

Assignment

  1. Complete set_tile(world, pos, ch). It replaces the tile at pos with ch by rebuilding that row and storing it back in world. It returns nothing.

  2. Complete pick_up(world, pos, backpack). If the tile at pos is a key in ITEMS:

    • append the item's name to backpack,
    • use set_tile to turn the tile into ".",
    • return f"Pip picks up the {name}!".

    Otherwise return "Nothing to pick up here."