
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
-
Complete
set_tile(world, pos, ch). It replaces the tile atposwithchby rebuilding that row and storing it back inworld. It returns nothing. -
Complete
pick_up(world, pos, backpack). If the tile atposis a key inITEMS:- append the item's name to
backpack, - use
set_tileto turn the tile into".", - return
f"Pip picks up the {name}!".
Otherwise return
"Nothing to pick up here." - append the item's name to
-
Complete
set_tile(world, pos, ch). It replaces the tile atposwithchby rebuilding that row and storing it back inworld. It returns nothing. -
Complete
pick_up(world, pos, backpack). If the tile atposis a key inITEMS:- append the item's name to
backpack, - use
set_tileto turn the tile into".", - return
f"Pip picks up the {name}!".
Otherwise return
"Nothing to pick up here." - append the item's name to
Press Run to try your code, then Check to test it.