← Teacher guides

Teacher guide: Deeper Python in the console

One idea at a time, in the console: text, lists, dictionaries and errors, then a text adventure built from all of them, saved to a file.

Open the student course

Each lesson works two ways. Students can work through the online tutorial at their own pace (it checks their answers and saves their progress in their browser), or you can teach it to the whole class from the slides, then send students into the tutorial for the Modify and Make steps.

Every lesson guide has plain-language notes, the answers to every check, a solution for every task, and slides and a worksheet for Waypoints Prepare. These guide pages are not linked from student pages, but they are not secret either: keep printed Teacher editions for answers you do not want students to find.

Whole-course cheat sheet: Open to print (every command, grouped by unit: a handout for the final challenge)

Working with text

Tidy, compare and cut up what people type.

Working with text in 5 minutes (read before teaching this unit)

The idea in plain words

Text (a string) has its own commands, called with a dot after it:

  • text.strip() removes spaces from both ends.
  • text.lower() and text.upper() change the capitals.
  • text.split() cuts the text into a list of words at the spaces.

Each one gives back new text and leaves the original alone, so store the result: command = command.strip(). They can be chained: input("> ").strip().lower().split(). A word is picked by its place, like any list: words[0] is the first word, and word[0] is the first letter of a word.

Why programmers use them

  • People type messily: extra spaces, capitals, more words than expected. Tidying first makes a program forgiving.
  • Comparisons are exact: "Yes" == "yes" is False, so tidy before comparing.
  • Commands are words: split() is the first step of every chatbot, search box and text adventure.

An everyday picture, and where it breaks

Tidying text is like a teacher reading a messy answer: ignore the smudges, then read the words. Where it breaks: Python does not read meaning. "Yes", "yep" and "sure" are three different texts; the program only understands what you check for.

Try it yourself (5 minutes)

  1. Run print(" Hello World ".strip().lower().split()).
  2. Run Lesson 1's parser and type TAKE lamp.
  3. Change the parser to accept y as well as yes with in ["yes", "y"].

Questions students ask

  • Why didn't my text change? The method gave back new text; store it with =.
  • Why is the first word words[0]? List places start at 0, as in the turtle course's lists.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Lists and dictionaries

Keep many things in a list, and look things up by name in a dictionary.

Lists and dictionaries in 5 minutes (read before teaching this unit)

The idea in plain words

A list keeps things in order and finds them by place. It can change while the program runs:

  • bag.append(x) adds to the end; bag.remove(x) takes x out (an error if it is not there).
  • x in bag is True or False; len(bag) is how many; bag.sort() puts it in order.
  • ", ".join(bag) shows a list of words neatly.

A dictionary finds things by name (its key) instead of by place: {"north": "kitchen", "east": "garden"}.

  • exits["north"] looks up the value; exits["west"] = "shed" adds or changes one.
  • "west" in exits checks for a key. Keys must match exactly.
  • A value can be a list or another dictionary: exits["hall"]["north"].

Why programmers use them

  • Lists for many things of the same kind in order: a bag, a queue, a playlist.
  • Dictionaries for looking things up: contacts, scores by name, a map from rooms to exits, counting words.

An everyday picture, and where it breaks

A list is a numbered queue; a dictionary is a phone's contacts, found by name. Where it breaks: contacts forgive small differences ("ada" finds "Ada"); a dictionary does not. "Hall" and "hall" are different keys.

Try it yourself (5 minutes)

  1. In Lesson 2, take three things, then type bag.
  2. In Lesson 3, walk the map, then add a shed to the east of the garden.
  3. Count the words in "the cat and the hat" by hand, then run the word counter.

Questions students ask

  • List or dictionary? By place and in order: a list. By name: a dictionary.
  • Why does my dictionary crash on a new word? counts["cat"] does not exist until you set it. Check with in first.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Errors and the adventure

Catch mistakes with try and except, build a text adventure in milestones, then save it to a file.

Errors, the adventure and files in 5 minutes (read before teaching this unit)

The idea in plain words

try and except. try: runs some lines; if one causes the error named after except, Python jumps there instead of stopping, then carries on:

try:
    n = int(text)
except ValueError:
    print("That is not a number.")

Name the error you expect (ValueError, KeyError, ZeroDivisionError); others still stop the program, which is what you want for real mistakes.

The text adventure joins the course's pieces: a loop that reads a command, a parser (strip, lower, split), the world as data (dictionaries of rooms, exits and things) and a bag (a list). Each command is one elif branch; each rule of the world is one check.

Files let a program remember after it stops. open(name, "w") empties a file and writes to it; open(name) reads it. json.dump(game, f) writes a dictionary as text and json.load(f) gives it back. The first time there is no file, which is a FileNotFoundError, caught like any other error. Lesson 6 runs in the Python playground (real Python, Pyodide), whose files are kept in the student's browser and shown in its Files tab.

Why programmers use them

  • Programs meet bad input, missing files and lost connections. Catching the error and carrying on sensibly is what makes software feel solid.
  • Games are data plus rules. Change the dictionaries and you have a new game; the loop stays the same.
  • Saving is writing the data down. Because the world is already data, saving it is one json.dump.

An everyday picture, and where it breaks

try is a safety net under one trick: it catches one kind of fall. A net for a ValueError does not catch a KeyError.

Try it yourself (5 minutes)

  1. Run Lesson 4's ticket program with three, 3.5 and -2.
  2. Play the finished adventure: find the key, open the shed, take the crown.
  3. Add a room to the adventure: one entry in rooms, exits and things, and an exit to it.
  4. Run Lesson 6's visit counter twice, reload the page, run it again, and open the Files tab.

Questions students ask

  • Why not catch every error with a bare except? It hides real mistakes, such as a typo in a variable name.
  • Why is the shed locked check before moving? The rule decides whether the move happens, so it must come first.
  • Where is the file? In the browser, for this course: it stays after the page closes, but not on another device or after clearing the browser's data. In Python on a computer it would be in the program's folder.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown