←Previous page

04CONDITIONALS · ELIF

One more question: elif.

elif is short for “else, if”. It asks a new question only when every question above it was False.

IN REAL LIFE

More than two choices, every day.

You ask your favorite first. If it is not there, you ask for the next one.

What is for lunch?

If there is pizza, I pick pizza. Else if there is pasta, I pick pasta. Else, a sandwich.

Which game?

If 6 friends come, we play soccer. Else if 2 come, we play tag. Else, I build with blocks.

Ice cream flavor

If they have chocolate, I take it. Else if they have strawberry, I take that. Else, vanilla.

LOOK FIRST

A ladder of questions.

if asks first

The first question always goes with if.

elif asks next

If the answer above was False, elif asks another question. You can add as many as you need.

else catches the rest

If every answer was False, the else path runs. It is optional.

STEP BY STEP

Follow the traffic light.

PyBot reads one question at a time, from the top.

light = "yellow"A box keeps the word yellow.
if light == "green":Is it green? No. Go down to the next question.
print("go")Skipped.
elif light == "yellow":Is it yellow? Yes!
print("slow down")This path runs.
elif light == "red":Not even asked. A path already ran.
print("stop")Skipped.
else:Skipped. else only runs when every answer was False.

LOOK FIRST

Three rules for elif.

The first yes wins

Python stops at the first True. The questions below it are not asked.

Order matters

Put the hardest question first. score >= 9 goes before score >= 5.

Two ifs are two questions

With two separate ifs, both can run. With elif, only one path runs.

RUN IT

Give PyBot a medal.

Guess first. Then run real Python and check.

1

Guess the result

score = 7 if score >= 9: print("gold") elif score >= 5: print("silver") else: print("bronze")

What will Python show?

medal.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change 7 to 10, then to 2. Which medal does PyBot get each time?

YOUR TURN

Climb the ladder of questions.

Try each tiny question. A wrong answer becomes something to review.

0Completed
10Not tried
0Review
01

Lunch menu

PyBot's rule: if there is pizza, pizza. Else if there is pasta, pasta. Else, a sandwich. Today there is pasta and sandwiches, but no pizza.

What does PyBot eat?

02

A short word

What does elif mean?

03

Red light

light = "red" if light == "green": print("go") elif light == "yellow": print("slow down") elif light == "red": print("stop")

What does Python show?

04

Two answers are True

n = 15 if n > 5: print("big") elif n > 10: print("huge")

What does Python show?

05

Fix the order

In the code above, PyBot wants 15 to show huge.

What should PyBot change?

06

Every answer is False

pet = "fish" if pet == "dog": print("woof") elif pet == "cat": print("meow")

What does Python show?

07

Two separate ifs

battery = 90 if battery > 50: print("play") if battery > 80: print("dance")

Which words show?

08

How many elifs?

How many elif lines can one if have?

09

Fix PyBot's code

Python does not know else if on one line. Use its short word.

Goal output
stop
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

elif adds more questions. The first True wins.

Next: when you compare one box with many values, Python has match.