S03 · Make the World React

What we learned in Python

The Python from session S03, one idea at a time. These notes follow lessons/sessions/s03/student/task-card.md in your course folder.

Back to the S03 slides

1 · Python concept

The idea

Today's Python
f-stringA string with an f right before the opening quote. Python fills in variables placed inside it.
braces { }Curly braces inside an f-string mark where a variable's value goes.
substitutionPython replaces {object_name} with the value stored in object_name: Moon Compass.
near vs. interactednear_message is the clue that appears when the player approaches. interacted_message is the reveal that appears after E is pressed nearby.

2 · Code we wrote

The code

object_name = "Moon Compass"
near_message = f"The {object_name} needle trembles toward the dark trees."
interacted_message = f"The {object_name} points past the trees to a guide's lantern!"

print(near_message)
print(interacted_message)

Run it from the course folder:

python lessons/sessions/s03/student/starter.py

Output:

The Moon Compass needle trembles toward the dark trees.
The Moon Compass points past the trees to a guide's lantern!

3 · What the code means

Line by line

Each important line
near_message = f"{object_name}"The starter's unfinished line. Before you add a clue, the starter prints only Moon Compass on its first line.
f"The {object_name} needle trembles toward the dark trees."The f makes this an f-string. {object_name} becomes Moon Compass; the rest is your clue text.
interacted_message = f"..."The reveal line, built the same way. It is the second line printed.
print(near_message)Shows the finished clue, with the object name already filled in.
when_near / when_interactedThe YAML fields in objects/compass.yaml. Copy near_message's text into when_near and interacted_message's text into when_interacted. YAML gets plain text, with no braces.

4 · Why programmers use this

Beyond this lesson

f-strings build a message from values you already have. Change object_name once and every message that uses it follows.

They keep text readable: you see the whole sentence, with each variable right where its value will appear.

Outside Explore Studio: an app greeting "Welcome back, Sam!", a score line in a game, or a weather report that fills in today's temperature are all built the same way.

5 · What we debugged

A bug worth understanding

near_message = f"The {object_name needle begins to shimmer."
SyntaxError: invalid syntax. Perhaps you forgot a comma?

The closing } after object_name is missing, so Python reads the rest of the line as code instead of text, and that code makes no sense. The error above is from Python 3.13; other versions may word the message differently, but it is always a SyntaxError on the f-string line.

Fixed:

near_message = f"The {object_name} needle begins to shimmer."

6 · Key Python words

Words to know

Vocabulary
f-stringA string starting with f" that can contain {variables}.
bracesThe curly brackets { and }. Every opening { needs a closing }.
substitutionReplacing a variable name with its value.
when_nearThe YAML field shown when the player approaches.
when_interactedThe YAML field shown after E is pressed nearby.
SyntaxErrorPython cannot read the code because it breaks a writing rule.

7 · Try it yourself

Your turn

  1. Predict, then test: change object_name to "Star Map". What changes in both printed lines?
  2. Remove the f before the opening quote, run the file, and compare the output. What happens to the braces?
  3. If the two YAML message values were swapped, what would a player see when moving near? When pressing E?
  4. Write a second, harder clue that hints at the same reveal with less detail.