Back to computer sciencecomputing use cases

Computer ScienceComputingMiddle schoolKey Stage 3

How to teach if-then statements

If-then statements let a program choose what to do. In Python, if, elif and else run one block of code depending on which condition is true. This page explains how they work, with a free activity where students code a choose-your-path story and Sparky tests the paths they skipped.

Title card reading 'How to teach if-then statements', next to three chat cards: a student tests her haunted house story only with 'upstairs', Sparky asks what it prints for 'Upstairs' with a capital U, and she fixes it with .lower().

"Flint was mostly employed by students as an easy-to-reach one-on-one tutor. The fast, non-judgmental comments helped them swiftly improve their writing and coding tasks."

Jason GrahamJason Graham, Social Studies/Inquiry Teacher at Jakarta Intercultural School

Why this matters

A student writes three branches for her story, runs the program once with the answer she expects, and calls it done. The bugs sit in the branches she never ran: a capital letter, an extra space, an answer the story doesn't cover. Her program looks finished, and the teacher has to wait until every program is graded to find out which ones break.

If-then statements in Python

An if-then statement runs a block of code only when a condition is true. In Python, an if line checks the first condition, each elif line (short for "else if") checks another one in order, and an else line runs when none of them matched (Python tutorial). Python runs exactly one branch: the first one whose condition is true. When nothing matches and there is no else, none of the branches run (Python language reference). A comparison such as choice == "left" matches only that exact text, so "Left" and "left " fall through to another branch until the code changes them with the string methods lower() and strip(). Geometry classes use the same words for conditional statements ("if p, then q"), and this page is about if-then statements in code.

In the Flint activity, each student codes a short choose-your-path story with if, elif and else, runs it in Flint's code editor and sends it to Sparky. A worksheet shows one branch to trace, and Sparky asks what each student's own program prints for answers they never tried.

Sparky gets Nadia to test the answers she skipped and fix them herself

Nadia ends up testing answers she never tried and making her story handle any capitalization with .lower(). Everyone in the class gets the same activity, and Sparky's replies depend on each student's own code and answers.

Sparky asks about a path Nadia didn't testSparky sends Nadia back to her own codeA right fix gets a harder ask
N

Nadia

Interest: Ghost storiesStarting point: Tests only the path she expects

Sparky

Type an answer and watch which branch lights up. Try typing Left with a capital L and see what happens!
An interactive Sparky attached to the greeting. A short Python program checks if answer equals left, elif right, elif forward, else. The student typed Left with a capital L, so the else branch is highlighted and a note says Python is case-sensitive.
InteractivePython if/elif/else Path Tracer

Nadia

here is my haunted house story. i ran it with upstairs and it works!
Nadia's Python program in Flint's code editor. It sets choice to upstairs, then has if choice equals upstairs, elif hide and elif run, each printing a different ending, and no else. The output shows You climb the stairs. A ghost girl hands you a rusty key.
CodeNadia's first version

Sparky

Now let's test a tricky case. What does your program print if you set choice = "Upstairs" with a capital U?

Nadia

with Upstairs it only printed the first line, the ghost girl part disappeared. so i fixed it, i added another elif for Upstairs and now it works

Sparky

Take a look at your choice variable. Is there a way to transform it before you compare it, so capitalization doesn't matter at all?

Nadia

i looked it up, .lower() makes it all lowercase. i took out the extra elif and tried UPSTAIRS and it works

Understanding check

Now for a harder challenge: pick one of your paths and add a second question inside it. That's a nested if inside your first if block.

Nadia

ok i added a second question inside upstairs. i tried open and it finds the diary!

Tested one answer, then fixed capitals herself and added a nested if

Nadia is an example student, and Sparky's replies are shortened.

What Flint did for Nadia

  • When Nadia tested only "upstairs", Sparky asked what her program prints for "Upstairs" with a capital U.
  • When she patched it with a second elif, Sparky sent her back to her choice variable without giving the fix.
  • Once .lower() fixed her capitals, Sparky asked for a harder version with a second question nested inside one path.

A teacher can't do this by hand for 30 students, and Sparky does it in every student's chat.

The activity page shows whose stories skip a path

While the class works, Needs attention shows the teacher which student is stuck and on which message. After three or more students submit, activity analytics lists what the class did well, where students went wrong, and a follow-up to practice next.

Flint's activity page for the choose-your-path story activity. Needs attention shows a student who ran his program only with the answer he expects. Activity analytics says 15 students added an else for unexpected answers and 8 added a new elif for each capitalization instead of using .lower(). The Sessions table lists three students.

Example activity page

After a class codes their choose-your-path stories

The school and students are made up for this page, and clicking a student opens their chat and the story they coded.

Set up the activity for free

The choose-your-path activity is in Flint's public library, with the Path Tracer, Sparky's guidelines and the greeting already written. Select Use activity to copy it into a free Flint account and assign it to a class. Students open the code editor with the Write code button and pick Python from its language menu, and since the editor can't read typed answers, they store the reader's answer in a variable such as choice = "hide" instead of using input().

For a project where each student picks the program to write, see how to run coding projects in middle school. For more computer science activities, see Flint for computer science.

Other use cases

See all use cases →

Learning feels different when it fits you.