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.
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)
Tidy, compare and cut up what people type.
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.
"Yes" == "yes" is False, so tidy before comparing.split() is the first step of every chatbot, search box and text adventure.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.
print(" Hello World ".strip().lower().split()). TAKE lamp.y as well as yes with in ["yes", "y"].=.Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown
Keep many things in a list, and look things up by name in a dictionary.
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.exits["hall"]["north"].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.
bag.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
Catch mistakes with try and except, build a text adventure in milestones, then save it to a file.
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.
json.dump.try is a safety net under one trick: it catches one kind of fall. A net for a ValueError does not catch a KeyError.
three, 3.5 and -2.rooms, exits and things, and an exit to it.Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown