S05 · Script a Conversation

What we learned in Python

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

Back to the S05 slides

1 · Python concept

The idea

Today's Python
listOne value that holds several items in order, written inside square brackets [ ] with commas between the items.
indexAn item's position number. Python counts from 0, so the first item is [0] and the second is [1].
[-1]A negative index counts from the end. dialogue[-1] is always the final item, however long the list is.
len(...)Counts the items in a list. A three-line conversation has len(dialogue) equal to 3.
Python to the worldThe dialogue list runs on your computer. The guide's lines in the Trail are plain text in the YAML conversation field, in the same order.

2 · Code we wrote

The code

dialogue = [
    "Guide: The dark stretch past the ridge won't clear.",
    "Guide: Three old marker-lights never burned out.",
    "Guide: Find those three lights and lead me home.",
]

print(dialogue[0])
print(dialogue[-1])
print(len(dialogue))

Run it from the course folder:

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

Output:

Guide: The dark stretch past the ridge won't clear.
Guide: Find those three lights and lead me home.
3

3 · What the code means

Line by line

Each important line
dialogue = [Starts a list named dialogue. Everything up to the closing ] is one value: the whole conversation.
"Guide: The dark stretch past the ridge won't clear.",Item 0, the situation. Each line is a string, and a comma separates it from the next item. In the starter it says "TODO: write an opening line" until you write your own.
"Guide: Three old marker-lights never burned out.",Item 1, the optional middle clue. A two-line conversation leaves it out.
"Guide: Find those three lights and lead me home.",The final item, the task: what the explorer should do next.
print(dialogue[0])Shows the first line. The square brackets after a list name pick one item by its index.
print(dialogue[-1])Shows the final line. -1 still works if the list has two lines instead of three.
print(len(dialogue))Shows how many lines the list holds. The starter already prints this, so before your changes it prints 2.
conversation:The YAML field in character/guide.yaml: the same 2–3 spoken lines, in the same order, without the "Guide: " label. Press E beside the Moonlit Guide once per line to see each one in the speech bubble.

4 · Why programmers use this

Beyond this lesson

Lists keep related values together and in order, so one name can hold a whole conversation instead of three separate variables.

Indexes let you reach any position directly, and [-1] always reaches the end, even when the list grows or shrinks.

Outside Explore Studio: a music playlist, the messages in a group chat, and the lines a game character says are all ordered lists. Play them in a different order and the meaning changes.

5 · What we debugged

A bug worth understanding

print(dialogue[3])
IndexError: list index out of range

A three-line list has items at positions 0, 1, and 2. There is no position 3, so Python stops with an IndexError. Read the final traceback line for the error type, then move upward to the line naming starter.py. Use dialogue[-1] for the final line: it is correct for two lines or three.

Fixed:

print(dialogue[-1])

6 · Key Python words

Words to know

Vocabulary
listAn ordered collection of items inside square brackets.
itemOne value stored in a list.
indexAn item's position number, starting at 0.
zero-basedCounting positions from 0, not 1.
negative indexA position counted from the end: -1 is the final item.
lenThe function that counts a list's items.
IndexErrorPython's error for asking for a position the list does not have.

7 · Try it yourself

Your turn

  1. Predict, then test: what does print(dialogue[1]) show for your conversation?
  2. Swap two lines, predict how the story changes, run it, then restore the situation → clue → task order.
  3. Remove the middle line. What does len(dialogue) print now, and does dialogue[-1] still show your final line?
  4. In the Trail, press E once more after the final line. Which line comes next, and does M05 stay complete?