Week 5 · Session 1

Combining Conditions

Asking two questions at once: and, or, not.

One Decision in Lab 2, Two Ways to Write It

The ride home needs the answer code and enough battery. The lab lets you write that either way.

  • One if inside another: the battery question is only asked once the code question held
  • Or one if whose condition names both at once, joined by and
  • Both reach the same three endings. Today is about that word, and when each way is the better one

Combining Conditions: and, or, not

  • a and b is True only when both sides are True
  • a or b is True when at least one side is True
  • not a flips True to False and back
  • 60 < temperature < 80 also works in Python: it is really two comparisons joined by and

Try It: Collapse the Nesting

This is Lab 2’s ride home, unchanged. Run it, then rewrite it.

  • Replace the nested version with one if that uses and, plus one else
  • Run all three answer pairs again: "no" with 100, "yes" with 10, "yes" with 80
  • Two outcomes collapse into one, so decide which message the new else should print

              

Try It: Two Conditions, One Decision

Run it with temperature 95. It prints "Mild," which is wrong.

  • or only needs one side to hold, and 95 satisfies the first half on its own
  • Change or to and so both bounds have to hold at once

              

When or Is the One You Want

Not every pair of questions is an and.

  • Use and when every condition has to hold: the code and the battery
  • Use or when any one of them is enough: did the lights at Lab 2’s tree line move, or did you hear something
  • A chain can be as long as you need, and or stops at the first side that is True

Nesting Is Still Right Sometimes

  • Reach for and when both questions lead to the same single outcome
  • Keep the nesting when the inner question has its own else worth telling apart
  • Lab 2’s ride home tells STRANDED from LEFT BEHIND: nest, or use and plus an elif. Both are right there
← Exit
1 / 7