← Objects with turtles

Objects with turtles: 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.

Many things on screen

1. Many turtles

Try firsta = Turtle(), then forward(50). Which turtle moves?

Check your answer

The default turtle, not a: forward(50) has no name in front. a.forward(50) would move a.

  1. a = Turtle() makes a new turtle and names it a. Each turtle is an object with its own position, heading and colour.
  2. Put the name and a dot in front of a command to give it to that turtle: a.forward(100) moves only a.
  3. A command with no name in front goes to the default turtle, the one from the turtle course.
Turtle() makes another turtle with its own name · Sound on for the voiceover
Open the lesson →

2. Lists of stars

Try firstnames = ["Ada", "Bo"] and ages = [12, 13, 11]. What is wrong?

Check your answer

The lists are out of step: three ages for two names, so the third age belongs to nobody (and a loop over ages would fail on names[2]).

  1. stars.append(s) adds each new star to a list, so one loop can move them all.
  2. speeds[i] and points[i] belong to stars[i]: the same place number, the same star.
  3. Lists side by side must be changed together. If one list has an item the others do not, you get an IndexError or a star using another star's values.
A list of turtles: one loop drives them all · Sound on for the voiceover
Open the lesson →

3. One dictionary per star

Try firstpet = {"name": "Rex", "legs": 4}. Write the line that changes legs to 3.

Check your answer

pet["legs"] = 3

  1. A dictionary pairs keys with values: {"speed": 5, "points": 2}.
  2. star["points"] reads the value under that key, and star["points"] = 3 changes it. Keys need quotes.
  3. Everything about one star stays in one dictionary, so adding a property is one more key, and nothing can fall out of step.
A dictionary keeps values together under names · Sound on for the voiceover
Open the lesson →

Classes

4. A Star class

Try firstclass Dog: def __init__(self, name): self.name = name. d = Dog("Rex"). What is d.name?

Check your answer

"Rex": __init__ stored it in d with self.name = name.

  1. class Star: describes a new kind of object, and Star(100) makes one.
  2. __init__ runs each time an object is made. self is the object being made, so self.speed = 5 stores a speed in that star.
  3. Read a value with the object's name and a dot: star.speed. Each star keeps its own.
class Star is the plan. Calling Star three times makes three star objects. Each one keeps its own values: star 1 has speed 4, star 2 speed 7, star 3 speed 3, and each has 1 point.
One class, many objects
A class is a pattern for making objects · Sound on for the voiceover
Open the lesson →

5. Stars that move themselves

Try firstclass Lamp: def __init__(self): self.on = False. Write a method switch that turns it on.

Check your answer

def switch(self): self.on = True (inside the class), called as lamp.switch().

  1. A method is a function inside a class. Its first parameter is self, the object it was called on.
  2. star.move() runs move for that star, so the star's own speed and turtle are used.
  3. Behaviour lives in the class, so the code that uses the objects stays short: for star in stars: star.move().
A method is a function that belongs to an object · Sound on for the voiceover
Open the lesson →