Grade machine: which branch runs, and why order matters
Move the score. Python checks the conditions from the top and stops at the first True. Then switch to the wrong order and watch whole grades disappear.
The path Python takes
Every score from 0 to 100 with this code
Boundary tests: the scores where bugs hide
What to notice
- Only one branch of an if / elif / else chain ever runs.
- Conditions after the first True are not checked at all.
- With the loosest condition first, A and B can never be printed: the code runs without error but is wrong.
- Test the edges: exactly 80, 79, 65, 64, 50, 49.
Try this
and · or · not: build conditions from smaller ones
Flip the two switches. Every expression below is recalculated. Click an expression to see its full truth table.
Your own expression (use p and q)
Truth table:
Real use: who can enter?
What to notice
andis True only when both sides are True;orwhen at least one is.not (p and q)always equalsnot p or not q. Compare their columns.- Each side of
andmust be a complete condition:age >= 18 and <= 60is a SyntaxError.
Try this
Spam rule tester: write a rule, test it on an inbox
Pick suspicious words. The rule flags a message if it contains them. See which spam it catches, which it misses, and which normal messages it wrongly flags.
Suspicious words (click to switch on or off)
Test your own message
The inbox (label = what a human decided)
What to notice
- More words with
orcatch more spam and raise more false alarms. - Switching to
andmakes the rule strict: few false alarms, but most spam slips through. - Some spam (FR33 g1ft) beats every word rule. Lecture 6 lets the computer learn the rule from examples instead.
Try this
match-case: one value, many fixed options
Type a phone number. Python looks at the first three characters and tries each case from the top, just like an elif chain.
Output
The same decision with if / elif
What to notice
number[:3]is the first three characters, as text:"017", not the number 17.|inside a case means or;case _:catches everything else, likeelse.- Without the
else:, a short number prints INV and then an operator name.