Week 4 · Session 1

Control Flow I

Teaching a program to make a decision: if, elif, else.

Every Comparison Is a Question

Python answers it with one of exactly two values.

  • 5 > 3 evaluates to True. 5 < 3 evaluates to False
  • == asks "are these equal." != asks "are these different"
  • <= and >= allow the equal case; < and > do not
  • Python writes ≥ as >= and ≤ as <=: two characters, and always in that order
  • True and False are values, the same way 26 and "hi" are values

Try It: Predict, Then Run

Guess each result before you press Run.

  • All four are comparisons. None of them change temperature
  • Then change 72 to 60 and predict all four again before rerunning

              

You Have Already Written a Block

Every program you have turned in this term starts with one.

  • def main(): ends in a colon, and every line of your program sits indented underneath it
  • That indented group is a block: the lines that belong to the thing above them
  • Python has no braces and no end keyword. The indentation is the only mark
  • Four spaces per level. VS Code inserts them when you press Tab

The if Statement

The same two pieces you already use, pointed at a condition.

  • if temperature > 90: ends in a colon, exactly like def main(): does
  • Everything indented under it is its block, and that block runs only when the condition is True
  • A line below it that is not indented is not part of the if. That line runs either way
  • Python decides what belongs to the if by how far a line is indented

Try It: One Symbol, Two Jobs

Run it first. Python refuses before your program even starts.

  • = stores a value. == asks a question. An if needs the question
  • The error names the exact line and even suggests the fix. Read it, then apply it

              

Try It: Fix the Indentation

Run it first. Nothing prints, and there is no error.

  • The condition is True. The line under it just is not part of the if
  • Indent it to match, then run again

              

else: The Other Branch

  • else: runs only when the if above it was False
  • Exactly one of the two branches runs
  • No condition goes on else: it means "everything the if did not cover"

Try It: Add the Other Branch

Right now, cold days print nothing at all.

  • Add an else that prints "Dress warm"
  • Then change temperature to 95 and confirm the first branch still runs instead

              

elif: More Than Two Outcomes

  • Short for "else if": another condition, checked only if every one above it was False
  • Python checks branches top to bottom and runs the first one that is True, then stops
  • Order matters: a wide condition placed first will catch cases meant for a later, narrower one

Try It: Order Matters

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

  • The > 50 branch catches 95 before the > 90 branch ever gets checked
  • Reorder the branches so the most specific condition comes first

              

One Decision Inside Another

A block can hold anything, including a second decision.

  • Put an if inside another if block and it is only reached when the outer one was True
  • Use it when the second question only matters once the first was answered yes
  • Each level of indentation is one more step in, and the indentation is the logic

Try It: The Sealed Door

Run it. There is no key, and the door opens anyway.

  • The two if statements are judged separately, so the charge alone is enough
  • Indent the second if and its print so they sit inside the first block, then run again
  • Then change has_key to "yes" and confirm the door opens

              

Before Session 2

This Is the Logic Lab 2 Runs On

Session 2: a real device. Session 3: the lab itself, released at lecture time.

  • Lab 2 lights the tower on Pig Hill, sorts a wind reading into the right range, and answers your ride home only when two things hold at once
  • Every decision in that lab is on this deck. Session 2 just gives it something physical to control
← Exit
1 / 14