Skip to content
Utah Community Learning

Combining conditions without tying your brain in a knot

About 20 minutes

Combining Conditions without Tying Your Brain in a Knot

Okay so here's the thing. You've got if, elif, else, and you can compare stuff with >, <, ==. That covers a lot of ground already. But real decisions are rarely just one condition. You don't just ask "is it raining." You ask "is it raining AND do I have somewhere to be."

That's what we're doing today. Stacking conditions together without the whole thing turning into spaghetti in your head.

The two words that do all the work

Python gives you and, or, and not. That's basically it. Super simple, on purpose.

  • and means both things have to be true.
  • or means at least one has to be true.
  • not just flips something. True becomes False, False becomes True.

Type this out yourself. Don't copy-paste it. Your fingers need to feel the syntax, not just your eyes.

```python age = int(input("How old are you? ")) has_id = input("Do you have an ID? (yes/no) ")

if age >= 21 and has_id == "yes": print("You're good.") else: print("Not happening.") ```

Run it a few different ways. Try age 25 with "no." Try age 16 with "yes." Watch it only say "You're good" when both things line up. That's and doing its job.

Now swap it for or:

``python if age >= 65 or has_id == "yes": print("Discount applies.") else: print("Full price.") ``

Now it only needs one of the two to be true. Big difference, one word.

Where this gets people stuck

Here's a super common mistake. People write something like:

``python if age == 16 or 17: ``

thinking that says "if age is 16 or 17." It does not say that. Python reads it as "if age equals 16, or if 17" and 17 is just always true as a number, so this condition is always true no matter what age actually is. It's a sneaky bug because it doesn't error out, it just quietly does the wrong thing forever.

What you actually want is:

``python if age == 16 or age == 17: ``

Yeah, it's more typing. Type it anyway. Clear beats clever every time, and I mean that as a real opinion, not a nice sentiment for a poster. I once wrote a slick one-line solution I was real proud of, showed it to my son, and a week later I opened the file and couldn't read my own code anymore. Clever is not the goal. Clear is the goal. Boring code that works is a heist you get to keep pulling off.

A word about naming things while you're in here

While you're writing these conditions you're going to want variable names, and I want to save you an hour of pain. Early on I named a variable list because, well, it was a list. Seemed obvious at the time. Everything downstream broke and I could not figure out why for a solid hour, because Python already uses the word list for its own purposes and I'd just overwritten it. Learned that lesson the loud way, staring at an error message that made no sense to me.

So when you're naming things for these exercises, stay away from words Python already uses. input, print, list, type, str -- these are all spoken for. If your variable name turns a weird color in your editor, that's usually the editor tapping you on the shoulder telling you something.

Practice this at home

Try building a tiny "can I drive to the canyon today" checker:

```python has_snow_tires = input("Snow tires on? (yes/no) ") road_closed = input("Is the road closed? (yes/no) ")

if road_closed == "yes": print("Nope, not today.") elif has_snow_tires == "no": print("Get snow tires first.") else: print("You're clear, go enjoy it.") ```

Notice I didn't jam this into one giant condition with a bunch of ands and ors stacked up. I broke it into steps, checking the road closure first because that one overrides everything else. Break your problem into stupid-small steps. People get stuck trying to write the whole decision in one line. You don't have to be clever, you just have to get the order right.

One real caution here, nothing dramatic: when you start combining and and or in the same line, use parentheses to group things, like (age >= 21 and has_id == "yes") or has_pass == "yes". Python has rules for which one it checks first if you don't, and those rules are not intuitive. Save yourself the guessing and just group it yourself.

Before next time

Take one decision from your actual life this week, something with two or three factors, like whether you go up the canyon or stay home, and write it as an if/elif/else. Doesn't have to be pretty. Just has to run.