Skip to content
Utah Community Learning

else and elif: when there's more than one answer

About 20 minutes

else and elif: when there's more than one answer

Okay so here's the thing. Last lesson you wrote your first if statement, and it probably looked something like checking if someone answered "yes." That's great, but real life doesn't only have two options, and neither does real code. Sometimes you've got three, four, ten possible answers. That's what else and elif are for.

Let's start with else, because it's the easy one.

else: the catch-all

You already know this pattern:

```python answer = input("Do you like pizza? ")

if answer == "yes": print("Same.") ```

Cool, that works if somebody types "yes." But what if they type "no"? Or "eh"? Or "obviously"? Right now, nothing happens. The program just shrugs and moves on. else gives it something to do in every other case:

```python answer = input("Do you like pizza? ")

if answer == "yes": print("Same.") else: print("Interesting choice.") ```

Now every possible answer leads somewhere. if handles the one case you're checking for, else handles literally everything you didn't think of. That's the whole job of else. It's not clever, it's just a safety net, and I like safety nets.

elif: when "everything else" isn't good enough

Here's where it gets more real. Sometimes you don't just have "yes" and "everything else." You've got a bunch of specific answers you actually care about. That's where elif comes in, short for "else if."

Say you're asking what somebody wants to drink:

```python drink = input("What do you want? ")

if drink == "water": print("Boring but healthy.") elif drink == "soda": print("Living dangerously.") elif drink == "coffee": print("A person of taste.") else: print("I don't have that one, sorry.") ```

Python checks these top to bottom, in order. First it checks if drink == "water". If that's false, it checks the next elif. If that's false, the next one. The second something matches, it runs that block and skips the rest. It doesn't keep checking after it finds a hit. That trips people up at first, so type it out and watch it happen for yourself.

a super common mistake

People write a pile of separate if statements instead of if / elif / elif. Like this:

``python if drink == "water": print("Boring but healthy.") if drink == "soda": print("Living dangerously.") ``

This looks almost the same but it isn't. Python checks every single if on its own, top to bottom, no skipping. Usually it doesn't bite you, but sometimes it does, and you get weird double-printing or logic that doesn't make sense. Using elif tells Python "these are all related, only run one of them." Separate ifs tell Python "check these completely independently." Know which one you mean.

the caveman thing

My son Jared, who is 21 and thinks he's the resident computer genius of the house, watched me debugging one of these multi-branch messes a while back. I had prints stuffed everywhere trying to figure out which branch was actually firing. He looks over my shoulder and goes "oh, you're using print statements like a caveman." Cheeky kid. He's not wrong, it is a caveman move. It's also the fastest way I know to answer the question "which one of these branches did the computer actually go into," so I'm still doing it and I'll keep doing it. Sometimes when you've got four or five elifs stacked up, the easiest way to sanity-check yourself is to drop a print() in each block that just says which one ran. Ugly, works.

try it yourself

Type this out, don't copy-paste it, your fingers need the reps:

```python score = input("What grade did you get, as a number 0-100? ") score = int(score)

if score >= 90: print("A. Nice.") elif score >= 80: print("B. Solid.") elif score >= 70: print("C. It's fine.") else: print("Let's talk.") ```

Notice the order matters here too. If you checked score >= 70 first, a 95 would stop right there and never get called an A, because Python quits checking once something matches. Order your elifs the way you'd actually reason through the problem, most specific or highest-priority case first.

Run it a few times with different numbers. Try 95, try 82, try 40. Watch which branch lights up each time.

one opinion, since we're here

You don't need to be good at math to write this stuff. I know it looks math-adjacent with the >= symbols, but this is really just "compare two things and go a different direction depending on the answer." That's closer to sorting laundry than solving equations. If numbers scare you a little, ignore that feeling for now, it's not really what's happening here.

Before next time: write one if / elif / elif / else chain about something dumb and personal, favorite season, favorite pizza topping, whatever. Doesn't matter what it is, just get the pattern into your hands.

else and elif: when there's more than one answer · Introduction to Coding with Python · Utah Community Learning