Skip to content
Utah Community Learning

if statements and asking yes-or-no questions

About 20 minutes

If Statements and Asking Yes-or-No Questions

Okay so here's the thing. Every program you've written so far does the exact same thing every single time you run it. Same greeting, same math, same total. That's fine, but it's also kind of dumb. Real programs need to make decisions. That's what this whole module is about, "if this, then that," and today we start with the "if" part.

An if statement is Python asking a question and then only doing something when the answer is yes. That's it. That's the whole concept. Everything else is just dressing it up.

The basic shape

Here's what it looks like:

```python age = int(input("How old are you? "))

if age >= 18: print("You can vote.") ```

Type this yourself. Don't copy-paste it. I say that every lesson and I'm going to keep saying it because your fingers actually learn something your eyes skip right past.

A few things to notice, because Python is picky about all of them:

  • There's a colon at the end of the if line.
  • The print line is indented. Usually four spaces, or one tab, whichever your editor gives you when you hit tab. Python cares about this indentation. It's not decoration, it's how Python knows what belongs to the if statement.
  • >= means "greater than or equal to." Regular math symbols mostly work the way you'd expect, <, >, == for "is equal to" (two equals signs, not one, we'll get to why some other day), and != for "is not equal to."

Run it. Answer with a number under 18 and notice nothing prints. That's correct behavior. The question got asked, the answer was no, so Python skipped the indented part entirely.

Adding the "otherwise"

Most of the time you want something to happen either way. That's else.

```python age = int(input("How old are you? "))

if age >= 18: print("You can vote.") else: print("Not yet, but you'll get there.") ```

Now every answer gets a response. One path or the other, never both. That's the whole idea of if/else, it's a fork, not a checklist.

Yes-or-no questions specifically

A super common thing beginners want to build is a program that asks a yes-or-no question and reacts to it. Something like:

```python answer = input("Do you want fries with that? ")

if answer == "yes": print("Good choice.") else: print("Suit yourself.") ```

Try it. Then try typing "Yes" with a capital Y and watch it fail, it'll fall into the else even though you meant yes. This trips up basically everyone at first. Python thinks "yes" and "Yes" are two completely different words, because to a computer they are, they're different letters in a different order as far as it's concerned. We'll deal with this properly in a later lesson with something called .lower(), but for now just know: if your yes-or-no program is acting weird, check whether you typed the exact word it's checking for. Nine times out of ten that's the bug.

A real one I built

Camille asked me to write something to track our grocery spending after a Costco run got a little out of hand. Nothing fancy, just something that would look at what we spent and tell us if we were over budget for the month.

```python budget = 600 spent = int(input("How much did we spend this month? "))

if spent > budget: print("Over budget.") else: print("We're fine.") ```

The program worked exactly right. It told us, correctly, that we spend way too much on cheese. Which, fine, we already knew that, the program didn't discover anything new, it just confirmed it in a slightly judgmental way. That's the thing about if statements, they're not smart, they're just honest. You ask a clear question, you get a clear answer. No feelings involved, no reading between the lines. Some days that's exactly what you want from a machine.

Try this at home

Write a program that asks the user's age and prints a different message for under 13, 13 to 17, and 18 and up. You'll need more than one if. There's a tool called elif (short for "else if") that handles exactly this, stack multiple questions in a row. Look up the syntax if you don't remember it, I don't have it memorized either and I write this stuff every week.

One thing I'll say plainly since it trips people up here specifically: don't try to write the whole three-way branching thing in your head first and then type it all at once. Get the first if working. Run it. Then add the elif. Then add the else. Small stupid steps, one at a time. That's basically the only trick to any of this.

Before next time

Play around with == versus > and < on both numbers and words, see what breaks and what doesn't. Half of learning this stuff is just poking it until it complains.