Skip to main content

Stage 3: It Reacts

Course progressStage 3 of 10
~30 min
Build

an assistant that responds differently to different commands

Learn

how if / elif / else lets a program choose what to do

Ship

a bot that answers hello, bye, and anything else in its own way

The big idea

So far your assistant always does the same thing. This stage it learns to choose — to look at what you typed and pick a different reply.

The tool for choosing is the if statement. An if runs some code only when a question is true. The question is called a condition — a yes/no test, like "did the user type hello?" We write that test with ==, which asks "are these two things equal?"

Three keywords work together:

  • if — check the first condition.
  • elif (short for "else if") — if the first was false, check another.
  • else — if none of the conditions were true, do this instead.
New words
condition
A yes/no question the program asks, like command == "hello".
==
Asks 'are these equal?' One equals sign stores a value; two equals signs compares.
if / elif / else
Choose what to run: if checks first, elif checks next, else covers everything left over.
Before you start

Make sure you finished Stage 2: It Knows You — your assistant should greet you by name.

Build it

Step 1 — Read one command

First, after the greeting, ask the user for a command and store it in a variable. A command is just a word the user types to tell the assistant what to do.

Python code task
Add to your file

assistant.py

Where it goes: Add this under your greeting lines, at the bottom of the file.

This reads one command. Next step decides what to do with it.

speak("Type a command: hello or bye.")
command = input("> ")

Step 2 — React differently to each command

Now the choosing. We check the command against "hello", then "bye", and if it's neither, we fall through to else.

Trace it

Trace the decision

  1. Python checks if command == "hello". If the user typed hello, it speaks the hello reply and skips the rest.
  2. If not, it checks elif command == "bye". If that's true, it speaks the goodbye and skips the rest.
  3. If neither was true, the else runs — the catch-all reply.
Think first

One equals or two?

To check whether the user typed hello, do you write command = "hello" or command == "hello"? Why?

Check your thinking

command == "hello" — two equals signs. One equals sign would try to STORE "hello" in command. Two equals signs ASK whether they're equal.

Python code task
Add to your file

assistant.py

Where it goes: Add this right under the command = input("> ") line.

Exactly one of these three branches runs each time.

if command == "hello":
speak(f"Hello again, {name}!")
elif command == "bye":
speak("Goodbye for now!")
else:
speak("I don't know that one yet.")

Run it. Type hello — it greets you. Run it again and type bye — a different reply. Run it once more and type banana — the friendly "I don't know that one yet." Your assistant is reacting.

Notice the indentation: the lines under each if/elif/else are pushed in. That spacing is how Python knows which lines belong to which branch. Line them up carefully.

Understand it

Here's the honest truth about your "smart" assistant: it doesn't understand the word hello. It just compared two strings and found they matched. That's it. It looks smart because you wrote good rules for it to follow — and writing good rules is exactly what real programming is. That's a superpower, not a letdown.

We used else as a catch-all on purpose. Without it, typing anything unexpected would make the assistant say nothing, which feels broken. A good assistant always answers, even if the answer is "I don't know that yet." Covering the "everything else" case is a habit that keeps programs feeling friendly instead of dead.

Try this

Learning beat

Try this

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

Predict first

What does the assistant say if you type Hello with a capital H? Predict first, then try it. (Hint: == is picky about capital letters — we'll fix that in Stage 8.)

Compare

Add an elif command == "joke": branch that speaks a joke you make up. Does adding a third choice work the same way as the first two?

Connect

Right now the assistant reads exactly one command and then the program ends. What if you wanted to give it command after command without restarting? (Stage 4 makes it keep listening.)

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 3.

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}.")

speak("Type a command: hello or bye.")
command = input("> ")

if command == "hello":
speak(f"Hello again, {name}!")
elif command == "bye":
speak("Goodbye for now!")
else:
speak("I don't know that one yet.")
  • Typing hello gives the hello reply.
  • Typing bye gives the goodbye reply.
  • Typing anything else gives the catch-all reply (no silence).
  • Design check. Read your else line. Is it friendly, or does it sound like an error? Make it sound like your assistant.
  • From memory. Without looking, write an elif branch for the command thanks. Did you indent the speak(...) line under it?

If it breaks

  • SyntaxError on the if line. The line must end in a colon :. Check that each of if, elif, and else ends with one.
  • IndentationError. The line under an if/elif/else must be pushed in (indented). Each branch's reply lives on an indented line beneath it.
  • It always says "I don't know." You probably used one = instead of two. Comparisons need ==.
  • Hello doesn't match hello. That's expected for now — == cares about capital letters. Stage 8 teaches the assistant to ignore them.