Quiz
Warm-up from lesson 3-2: what does `input()` return when the user types 42?
The full decision shape
Everything you have written so far runs the same lines on every run. Real programs earn their keep by deciding: is this password correct, does this order qualify for free shipping, which band does this score fall in. The if statement is how a program takes different paths for different data, and it appears in essentially every function you will ever read.
An if statement runs its indented block only when its condition is True. The complete shape has three parts:
score = 83 if score >= 90: print("A") elif score >= 80: print("B") elif score >= 70: print("C") else: print("F")
elif means else if: checked only when everything above it was False. else catches whatever is left. Python tests the conditions top to bottom and runs exactly one branch, then jumps past the whole chain. That is why score >= 80 is safe as the second test: reaching it already proves the score is below 90.
Comparisons: == equal (two signs, = alone is assignment), != not equal, <, >, <=, >=. Indentation is not decoration in Python, the 4 spaces are how it knows what belongs to each branch.
Code exercise · python
Run the grading chain. Then try changing `score` to 95, 71, and 50 and predict the letter before each run.
Code exercise · python
Your turn. Write an elif chain for temperature `t`: below 0 print `freezing`, below 15 print `cold`, below 25 print `mild`, otherwise print `hot`. With t = 3 it should print `cold`.
Problem
In this lesson's grading chain, what letter does `score = 90` print? (Boundary values are where grading bugs live, so check the comparison carefully.)