Gatekeeper — if, elif and else
You are the gate. Write the rule, and every visitor in the table is checked against it.
How to play
Each level gives you a function and a table of cases with the verdict each one should get. Write the condition, press Run, and every row is marked individually. One red row tells you far more than a single pass-or-fail would: it names the exact input your logic gets wrong, which is usually a boundary.
What each level teaches
- Over eighteen — that a comparison is already a boolean.
return age >= 18replaces four lines of if/else. - Grade the paper —
elifchains, and why ordering them from the top band down means each test needs only one bound. - Members only —
and, true only when both sides are. - Price the ticket — bands with an
else, and boundary values: is 13 a child, is 65 a senior? - Three conditions —
not, and whynot suspendedreads better thansuspended == False. - Sort the character —
infor membership, which replaces a pile oforcomparisons.
Boundaries are where the bugs are
Look at the test tables: several rows exist only to pin down an edge. 18 and 17. 90 and 89. 13 and 12. 65 and 64.
Nearly every conditional bug in real software is one of these — > written where >= was
meant. When you write a condition of your own, the habit worth building is to immediately ask "what happens exactly
on the boundary?" and test that value specifically.
Two patterns worth stealing
- Return the comparison.
return score >= 50, neverif score >= 50: return True else: return False. The second form is longer and says the same thing. - Guard clauses. Handle the exceptional cases first and return early, so the main logic is not buried four indents deep. It keeps functions flat and readable.
The written version, with truth tables for and, or and not, is in the
tutorial on Python if, elif and else.
Frequently asked questions
What is the difference between if and elif?
elif is only tested when every condition above it was false. A sequence of separate ifs tests all of them, so more than one branch can run — which is usually not what you want for bands or grades.
Do I need an else?
No. An if without an else simply does nothing when the condition is false. Inside a function, though, a missing else often means the function returns None by accident.
What does and actually return?
Not necessarily True or False — it returns one of its operands. "a" and "b" is "b", and 0 and 5 is 0. In an if statement the result is tested for truthiness, so this rarely matters, but it explains some surprising output.
Why is `not suspended` better than `suspended == False`?
It is shorter, it reads as English, and it works correctly for any falsy value rather than only for the exact object False.
Can I write the conditions differently from the solution?
Yes. Every row of the table is checked independently, so any logic that gives the right verdict for every case passes.
Keep going
- Read the if, elif and else tutorial — the written explanation behind every level here.
- Variable Vault — variables & data types.
- Operator Arcade — numbers & operators.
- String Surgeon — strings & slicing.
- 20 Python practice exercises with worked solutions.
Take the games with you
Coding Python packs a real Python 3 compiler, 30 example programs, 112 quiz questions and 12 self-checking challenges into one free Android app.
Get it on Google Play