- Moving around three locations
Copy the following code into your IDE and run it.
places = ["A clearing in a forest", "An old wooden cabin", "A dark cave"] moves = [{"n": 1, "s": 2}, {"s": 0}, {"n": 0}] location = 0 def Main(): ans = "" global location print(places[0]) while ans != "bye": ans = input("What now?") if ans in moves[location]: location = moves[location].get(ans) print(places[location]) else: print("I can't move that way") Main()
Exercise
- How do you exit the game?
- What sort of data structure has been used to store the places?
- What sort of data structure has been used to store the moves?
- What does the variable location do?
- Give some valid moves.
- How does the code check if a move is valid?
- Which line of code sets the new location?
- Look at the map below. Add the extra locations to the game.
- Run the code and check that it works.