
Code
Teach Pip Commands
Players type messy things: " North", "E", "EAST ". A game
should understand them all. First, clean the text:
word = " North ".strip().lower() # "north"
Shortcuts with a dict
Many players type just one letter. A dict can swap a shortcut
(an alias) for the full word. .get(key, default) gives the
default when the key is missing:
ALIASES = {"n": "north", "s": "south"}
print(ALIASES.get("n", "n")) # north
print(ALIASES.get("take", "take")) # take
So ALIASES.get(word, word) means "use the alias if there is
one, otherwise keep the word".
Commands to moves
Another dict, MOVES, turns each direction into its delta.
MOVES.get(word) returns None when the word isn't a direction,
so unknown commands can't crash the game.
Pip tip: Try typing your own command in the Input box, like
worSOUTH.
Assignment
- Complete
normalize(text): strip spaces, make it lowercase, then return the full word if it's an alias (else the cleaned word). - Complete
delta_for(text): return the delta fromMOVESfornormalize(text), orNoneif it isn't a direction.
- Complete
normalize(text): strip spaces, make it lowercase, then return the full word if it's an alias (else the cleaned word). - Complete
delta_for(text): return the delta fromMOVESfornormalize(text), orNoneif it isn't a direction.
main.py Python sleeping
Press Run to try your code, then Check to test it.