Skip to main content

Stage 2: It Knows You

Course progressStage 2 of 10
~30 min
Build

an assistant that asks your name and greets you by it

Learn

how variables store information and how input() listens

Ship

a personalized hello that works for whoever runs the program

The big idea

Last stage, the assistant only knew its own name because you typed it into the code. This stage, it learns your name — by asking.

A variable is a labeled box that holds a value. You make one with a name, an equals sign, and a value: name = "Sam". After that, wherever you write name, Python swaps in "Sam".

To fill that box with whatever the user types, we use input(). The input() function shows a question, waits for the person to type an answer and press Return, and hands back whatever they typed. We catch that answer in a variable.

New words
variable
A labeled box that holds a value. name = "Sam" puts "Sam" in a box called name.
input()
A function that asks the user a question and gives back whatever they type.
f-string
A string with an f in front, like f"Hi {name}", that drops a variable's value right into the text.
Before you start

Make sure you finished Stage 1: Wake It Up — your assistant should introduce itself out loud.

Build it

Step 1 — Ask for a name and remember it

We'll ask the user their name and store the answer in a variable called name. Storing it means we can use it again and again without asking twice.

Think first

Where does the answer go?

After the line `name = input("What is your name? ")` runs and the user types Maya, what is inside the variable `name`?

Check your thinking

The string "Maya". input() hands back whatever the user typed, and the = sign stores it in the name box.

Python code task
Update your file

assistant.py

Where it goes: Replace your three intro lines with this. Keep the import and the speak function at the top.

input() pauses the program until the user types something and presses Return.

import subprocess


def speak(text):
print(text)
subprocess.run(["say", text])


speak("Hi! My name is Pixel.")

name = input("What is your name? ")
speak("Nice to meet you, " + name + "!")

Run it. Your assistant introduces itself, then asks for your name in the terminal. Type your name, press Return, and it greets you out loud — by name. Run it again and type a different name. Same code, different greeting.

Step 2 — Glue words together two ways

Look at "Nice to meet you, " + name + "!". The + joins strings end to end. That works, but counting spaces and plus signs gets fiddly fast.

There's a cleaner way: an f-string. Put an f right before the opening quote, then wrap any variable in curly braces { }, and Python drops its value right into the text.

Your turn

Rewrite it as an f-string

Add a second greeting line using an f-string. Notice how much easier it is to read than gluing with plus signs.

Need a hint?

f"I'm ready when you are, {name}." — the f goes before the quote, and {name} gets replaced by what the user typed.

Python code task
Add to your file

assistant.py

Where it goes: Add this line right under your 'Nice to meet you' line.

Both lines say the name — one with +, one with an f-string. The f-string is the style we'll use from now on.

speak(f"I'm ready when you are, {name}.")

Run it again. Two personalized lines, and the second one was much easier to type. From here on, we'll use f-strings whenever we mix words and variables.

Understand it

The assistant didn't get smarter by magic — it got one variable. Storing the name means the program can reuse it any time without asking again. That's what variables are for: remember something once, use it everywhere.

We chose the f-string over gluing with + for a real reason: code gets read far more often than it gets written. f"Hi {name}" says what it means at a glance, while "Hi " + name makes the next reader (probably future you) count spaces. Picking the more readable option now is a habit that pays off all week.

Try this

Learning beat

Try this

Three short experiments. Predict before you run, then test your guess.

Predict first

What happens if you type nothing and just press Return at the name question? Predict what the greeting will sound like, then try it.

Compare

Add a second question: mood = input("How are you feeling? "), then speak(f"I hope you feel {mood} all day, {name}."). Does using two variables in one f-string work the way you expected?

Connect

The assistant can ask a question and answer once. But what if you wanted to give it a command and have it react differently depending on what you said? (Stage 3 teaches it to choose.)

Test your stage

Stuck? Compare carefully
Answer check
Debug compare only

assistant.py

Where it goes: Compare this against your whole file. Use it only if your program won't run.

This is the full file at the end of Stage 2.

import subprocess


def speak(text):
print(text)
subprocess.run(["say", text])


speak("Hi! My name is Pixel.")

name = input("What is your name? ")
speak("Nice to meet you, " + name + "!")
speak(f"I'm ready when you are, {name}.")
  • The program asks for your name and waits for you to type.
  • It greets you out loud using the name you typed.
  • Running it again with a different name gives a different greeting.
  • Design check. Read your f-string line. Could a friend tell what it will say without running it?
  • From memory. Without looking, write a line that stores a favorite color in a variable called color. Did you use color = input(...)?

If it breaks

  • It greets an empty name. You pressed Return without typing. That's not a crash — the assistant just got an empty answer. Run it again and type something.
  • NameError: name 'name' is not defined. The input() line that makes the name box has to come before any line that uses name. Read your file top to bottom.
  • The braces show up literally, like {name}. You forgot the f right before the opening quote. An f-string only works with the f.
  • It prints but doesn't talk. Volume first — turn it up and unplug headphones.