Coding Python
Home›Python Coding Games›Gatekeeper

Gatekeeper — if, elif and else

You are the gate. Write the rule, and every visitor in the table is checked against it.

Level 1

Checks
    Your PythonPython 3
    1

    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

    1. Over eighteen — that a comparison is already a boolean. return age >= 18 replaces four lines of if/else.
    2. Grade the paper — elif chains, and why ordering them from the top band down means each test needs only one bound.
    3. Members only — and, true only when both sides are.
    4. Price the ticket — bands with an else, and boundary values: is 13 a child, is 65 a senior?
    5. Three conditions — not, and why not suspended reads better than suspended == False.
    6. Sort the character — in for membership, which replaces a pile of or comparisons.

    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, never if 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.

    Practise if, elif and else on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
    Get it free

    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

    ← All Python coding games  Read the tutorial →

    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
    Coding Python app icon