← Teacher guides

Teacher guide: Objects with turtles

Build a falling-stars game with many turtles, and tidy it step by step: from separate variables to lists, dictionaries and classes.

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)

Many things on screen

Many turtles at once, then lists and dictionaries to keep track of them.

Many things on screen in 5 minutes (read before teaching this unit)

The idea in plain words

Turtle() makes another turtle. Give it a name, star = Turtle(), and put the name and a dot in front of a command to give the command to that turtle: star.forward(50). Each turtle is an object: it keeps its own position, heading and colour. A command with no name in front goes to the default turtle, the one students used in the turtle course.

Once there are many objects, the program needs a way to keep track of them:

  • Separate variables (s1, s2, s3) work for three and cost the same block of code for every new one.
  • A list of turtles plus a loop handles any number. What each star needs to remember (speed, points) goes in more lists beside it, matched by place number. These lists side by side must always be changed together.
  • A dictionary keeps everything about one star together: {"turtle": s, "speed": 5, "points": 1}, read as star["speed"]. A list of dictionaries is a table: one row per star.

Why programmers use them

  • Games, simulations and apps are full of objects: enemies, bullets, buttons, messages.
  • Lists make the count a number: 4 stars or 400 is the same code.
  • Dictionaries keep a record together, so nothing falls out of step. Most data is stored this way, from spreadsheets to databases.

An everyday picture, and where it breaks

Lists side by side are like a class roll and a separate list of phone numbers, matched by row. They work until someone joins the class and only one list is updated. A dictionary is a contact card: the name and the number stay on the same card.

Try it yourself (5 minutes)

  1. Run Lesson 1's game, then count the lines that mention s1.
  2. In Lesson 2, change range(4) to range(8) and run it.
  3. In Lesson 3, add "colour": "orange" to the dictionary and colour each star with star["turtle"].color(star["colour"]).

Questions students ask

  • Why is there sometimes an extra turtle in the middle? A command without a name in front used the default turtle, so Python drew it.
  • Can a turtle be in a dictionary? Yes. A dictionary can hold any value, including a turtle.

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

Classes

Make your own kind of object with class, then give it methods: one line adds another star.

Classes in 5 minutes (read before teaching this unit)

The idea in plain words

A class describes a new kind of object. class Star: says what every star stores and what every star can do. Star(100) makes one star; make four and you have four separate stars from one pattern.

  • __init__ (say "init") runs each time a star is made. Inside it, self is the star being made, and self.speed = 5 stores a speed in that star.
  • Read an object's values with its name and a dot: star.speed, like a turtle's own commands.
  • A method is a function inside the class, with self as its first parameter: def move(self):. star.move() runs it for that star. Methods put each kind of object's behaviour in one place.

This course stops there: no inheritance, getters and setters, or class diagrams.

Why programmers use them

  • Many things of the same kind: one class, any number of objects.
  • Behaviour lives with the data: change how a star moves in one place and every star follows.
  • Programs read like the problem: star.move() and rock.touches(player) say what they mean.

An everyday picture, and where it breaks

A class is a cookie cutter and each object is a cookie: same shape, but each one can have its own icing (its own values). Methods are like a vending machine's buttons: you press "give drink" and the machine knows how.

Where it breaks: the cookie cutter does not make cookies by itself. A class does nothing until Star(...) makes an object, and its methods do nothing until they are called.

Try it yourself (5 minutes)

  1. In Lesson 4's Predict, change Pet("Rex", 4) to another pet and print its name.
  2. Remove self. from self.speed = ... in Lesson 4's game and run it: read the AttributeError.
  3. In Lesson 5, add print("caught") inside a new method catch(self) and call it when a star is caught.

Questions students ask

  • Why self? Python passes the object to every method. self is the name for "the star this method was called on".
  • Why two underscores in __init__? Python's names for special methods have them. __init__ is the one Python runs when an object is made.

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