Week 4 · Session 1
Teaching a program to make a decision: if, elif, else.
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>= and ≤ as <=: two characters, and always in that orderTrue and False are values, the same way 26 and "hi" are valuesGuess each result before you press Run.
temperature72 to 60 and predict all four again before rerunningEvery 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 itend keyword. The indentation is the only markif StatementThe same two pieces you already use, pointed at a condition.
if temperature > 90: ends in a colon, exactly like def main(): doesTrueif. That line runs either wayif by how far a line is indentedRun it first. Python refuses before your program even starts.
= stores a value. == asks a question. An if needs the questionRun it first. Nothing prints, and there is no error.
True. The line under it just is not part of the ifelse: The Other Branchelse: runs only when the if above it was Falseelse: it means "everything the if did not cover"Right now, cold days print nothing at all.
else that prints "Dress warm"temperature to 95 and confirm the first branch still runs insteadelif: More Than Two OutcomesFalseTrue, then stopsRun it with temperature 95. It prints "Warm," which is wrong.
> 50 branch catches 95 before the > 90 branch ever gets checkedA block can hold anything, including a second decision.
if inside another if block and it is only reached when the outer one was TrueRun it. There is no key, and the door opens anyway.
if statements are judged separately, so the charge alone is enoughif and its print so they sit inside the first block, then run againhas_key to "yes" and confirm the door opensBefore Session 2
Session 2: a real device. Session 3: the lab itself, released at lecture time.