S04 · Introduce a Character

What we learned in Python

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

Back to the S04 slides

1 · Python concept

The idea

Today's Python
functionA named, reusable set of steps. Define it once, then call it as many times as you need.
defThe keyword that defines a function: def, the function name, parentheses, then a colon.
parameterThe name in the definition's parentheses. In def greet(name):, name is the parameter.
argumentThe value in a call's parentheses. In greet("Ari"), "Ari" is the argument, and it becomes name.
Python to the worldgreet(name) runs on your computer. The guide's greeting in the Trail is plain text in the YAML greeting field.

2 · Code we wrote

The code

def greet(name):
    place = "Moonlit Trail"
    print(f"Welcome to {place}, {name}!")


greet("Ari")
greet("Sam")

Run it from the course folder:

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

Output:

Welcome to Moonlit Trail, Ari!
Welcome to Moonlit Trail, Sam!

3 · What the code means

Line by line

Each important line
def greet(name):Defines a function named greet with one parameter, name. Defining it does not run it; nothing prints yet.
place = "Moonlit Trail"The first body line. In the starter it says "TODO: name your setting" until you replace it with your setting name.
print(f"Welcome to {place}, {name}!")Uses the value inside the function: {name} becomes whatever argument the call sent in. Both body lines are indented by the same four spaces.
greet("Ari")A call. It runs the body with name set to "Ari" and prints the first line.
greet("Sam")Your second call, with a name you choose. Same body, different argument, different line.
greeting:The YAML field in character/guide.yaml. One sentence that tells who the guide is, what the trail problem is, and what help it needs. Press E beside the Moonlit Guide to see it in the speech bubble.

4 · Why programmers use this

Beyond this lesson

Functions let you write a set of steps once and reuse it. Fix or change the body, and every call gets the change.

Parameters make one function work for many values: the same greet body welcomes Ari, Sam, or anyone else.

Outside Explore Studio: a game that greets each player by name, or an app that sends "Happy birthday" to whoever's birthday it is, calls one function with a different argument each time.

5 · What we debugged

A bug worth understanding

greet()
TypeError: greet() missing 1 required positional argument: 'name'

greet has one parameter, name, so every call needs one argument to fill it. greet() sends none. Read the final traceback line for the error type, then move upward to the first line naming starter.py to find the line number. Fix the call; do not remove or add a parameter to hide the error.

Fixed:

greet("Kai")

6 · Key Python words

Words to know

Vocabulary
functionA named, reusable set of steps.
defThe keyword that starts a function definition.
parameterThe name in a definition's parentheses that receives a value.
argumentThe value placed in a call's parentheses.
callUsing a function's name with parentheses to run its body.
indentationThe four spaces that put lines inside the function body.
tracebackPython's error report: the error type on the last line, and the file and line above it.

7 · Try it yourself

Your turn

  1. Predict, then test: add greet("Kai"). What exact line will it print?
  2. Change place to your own setting name. How many printed lines change?
  3. Remove the four spaces before the print line, run the file, and read the error. Then put them back.
  4. Rewrite the guide's YAML greeting in one sentence using your three voice words, then read it in the speech bubble.