Good place to stop if you're short on time — step 3 picks up here.
Lesson idea Tap to fold
Without scrolling back — what tells Python which lines belong to the if block versus the else block?
Show the answer
Answer. The indent — same as a loop. Everything indented under if runs when the condition is True; everything indented under else runs when it's False.
And from further back — what is the difference between print and return?
Show the answer
Answer. print shows something on screen. return hands a value back so it can be used.
elif adds more forks in a row
if/else only ever gives you two paths. Most real decisions have more than two — a QC pass doesn't just pass or fail, it can also come back with a warning. elif — short for "else if" — chains extra forks in between:
# Set up the values for this step
error_count = 2
# Choose the matching result
if error_count == 0:
print("Pass")
elif error_count <= 3:
print("Warning")
else:
print("Fail")
Python checks each condition top to bottom: is error_count 0? No. Is it 3 or fewer? Yes — print "Warning", and stop. It never even looks at the else.
Python checks top to bottom and stops at the first True branch — order matters. If error_count were 0, the very first check would already be True, and everything below it — the elif, the else — would never run, even if they'd also have matched.
That means the most specific condition usually belongs first. Put a broad "catch anything" check at the top of the chain by mistake, and every narrower check below it becomes dead code — correct, but unreachable.
That is the whole idea for today. Chain as many elifs as a decision needs; Python runs exactly one branch, the first one that's True.
Predict, explain, then test
These three short checks are self-reviewed, not a quiz score. The code checks below rerun your rule on changed inputs and boundaries. They keep your named input facts and check your rule, not one required quote style.
Predict a boundary
For zero → Pass, up to three → Warning, otherwise → Fail, what happens at three and four?
Compare your answer · self-reviewed
Three is Warning; four is Fail. <= includes the three-error boundary.
Compare chains
Why can two separate if statements print two results at zero errors?
Compare your answer · self-reviewed
Both zero == 0 and zero <= 3 are True. elif connects them so the first matching branch wins.
Explain the order
Why test zero before testing up to three?
Compare your answer · self-reviewed
Zero also satisfies the broader warning condition. If that condition comes first, the specific Pass branch is never reached.
Choose an exercise to load its prompt.
Today's idea in two other voices, if you want one. Not required — the exercises above are the actual lesson.
- Conditional Statements in Python — article, Real Python
- Python Tutorial for Beginners 6: Conditionals and Booleans — video, Corey Schafer
Loading exercises…
Noted on your route map, with the step you were on. Nothing is lost by parking it — the next review day will bring this idea back, and the flag tells the course where to slow down.
The tutor mounts here when JavaScript is available. The lesson above stays readable without it.