← Deeper Python in the console

Deeper Python in the console: recap

For after the lessons: one card per lesson, to check what you remember. Answer each question in your head or on paper first, then check it. Nothing here is marked.

Working with text

1. Working with text

Try firstWhat does " Hello World ".strip().lower().split() give?

Check your answer

["hello", "world"]: spaces off the ends, small letters, then cut into words.

  1. strip() removes the spaces at both ends; lower() makes every letter small. Each gives back new text.
  2. split() cuts text into a list of words at the spaces, so words[0] is the first word.
  3. Tidy text before comparing it: "Yes" == "yes" is False.
Tidy the text, then cut it into words · Sound on for the voiceover
Open the lesson →

Lists and dictionaries

2. A bag that grows

Try firstbag = ["lamp"]. What happens with bag.remove("key"), and how do you avoid it?

Check your answer

It stops with a ValueError because key is not in the bag. Check first: if "key" in bag: bag.remove("key").

  1. bag.append("key") adds to the end of the list; bag.remove("map") takes it out.
  2. remove() stops with an error if the item is not there, so check first: if "map" in bag:.
  3. ", ".join(bag) shows the list neatly: lamp, key.
A list can grow and shrink · Sound on for the voiceover
Open the lesson →

3. A map of rooms

Try firstages = {"Ada": 12}. What do ages["Ada"] and "Bo" in ages give?

Check your answer

12, and False: Bo is not a key.

  1. A dictionary pairs keys with values: {"north": "kitchen"}. exits["north"] looks up the value.
  2. Keys must match exactly. Check first with in: "west" in exits is True or False.
  3. A value can be another dictionary: exits[here]["north"] looks up twice.
A dictionary looks a value up by its name · Sound on for the voiceover
Open the lesson →

Errors and the adventure

4. When things go wrong

Try firsttry: n = int("3.5") except ValueError: n = 0. What is n?

Check your answer

0: int("3.5") causes a ValueError, so the except line runs.

  1. try: runs its lines. If one causes the error named after except, Python jumps to the except lines instead of stopping.
  2. Name the error you expect: ValueError for a bad number, KeyError for a missing key, ZeroDivisionError for dividing by 0.
  3. Then the program carries on. In a loop, ask again until the answer works.
try runs code that might fail; except says what to do if it does · Sound on for the voiceover
Open the lesson →

5. A text adventure

Try firstWhich data structure would you use for the things in each room, and why?

Check your answer

A dictionary from room name to a list: look up the room by name, and the list grows and shrinks as things are taken and dropped.

  1. The world is data: rooms and exits are dictionaries, and things[here] is a list of what lies in the room.
  2. Each command is one elif branch in a loop. take moves a thing from things[here] to bag; drop moves it back.
  3. A rule is one more check in the right branch, such as a shed that stays locked while "key" not in bag.
The finished adventure: find the key, open the shed, take the crown · Sound on for the voiceover
Open the lesson →

6. Save the game

Try firstWhy does the game save things as well as here and bag?

Check your answer

things changes during play (taking moves an item out of a room), so without it a loaded game would put the item back in the room as well as in the bag.

  1. A file outlives the program. open(name, "w") empties the file and writes; open(name) reads.
  2. json.dump(game, f) writes a dictionary as text; json.load(f) gives the dictionary back.
  3. The first time there is no file, so except FileNotFoundError: starts afresh instead of crashing.
Save the game to a file, then load it in a new run · Sound on for the voiceover
Open the lesson →