16. if / elif / else — Homework solutions

The .py solution files are in exercises/16/homework/solutions/.

Problem 1 — Even or odd

Problem. Read a number; print even or odd.

How to think about it. n % 2 is 0 for even and 1 for odd (negatives give -1 or 1, but == 0 still works). One if/else.

Worked solution.

n = int(input("Enter a whole number: "))

if n % 2 == 0:
    print("even")
else:
    print("odd")

Common mistakes.

  • Writing if n % 2 = 0. Single = is assignment; the check needs ==.
  • Forgetting int(). n % 2 on a string is an error.

Problem 2 — Roblox level gate

Problem. Different messages depending on which of two requirements the player fails.

How to think about it. Two booleans give four combinations, but only three are bad. Spell each out with elif:

  • both ok → enter
  • level low, key ok → "Level too low."
  • level ok, key missing → "Missing the key."
  • both wrong → "Level too low and missing the key."

Branch order matters: handle the most specific failures first.

Worked solution.

level = 12
has_key = True

if level >= 10 and has_key:
    print("You can enter the dungeon.")
elif level < 10 and not has_key:
    print("Level too low and missing the key.")
elif level < 10:
    print("Level too low.")
else:
    print("Missing the key.")

The combined failure is checked before the single ones. If elif level < 10 came first, it would swallow the combined case, so that message never shows.

Common mistakes.

  • Using && or ||. Python uses and and or.
  • Forgetting that not has_key means the key is missing. Say it out loud: "not has key".

Problem 3 — Grade letter

Problem. Standard A/B/C/D/F based on cutoffs.

How to think about it. Each branch checks the highest cutoff not yet caught. With elif, once a branch matches, the rest are skipped.

Worked solution.

score = int(input("Score (0-100): "))

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")
else:
    print("Grade: F")

Common mistakes.

  • Listing the conditions from low to high. Putting score >= 60 first catches every passing score, including 95, and gives them all a D.
  • Using > instead of >=. A score of exactly 90 should be an A, not a B.

Challenge — Largest of three

Problem. Three numbers, no max(), no loops. Print the largest.

How to think about it. Compare them pair by pair. a >= b and a >= c means a is the largest, or tied for it. >= (not >) handles ties: if a == b, the first branch wins and prints a.

Worked solution.

a = 7
b = 12
c = 12

if a >= b and a >= c:
    print(a)
elif b >= c:
    print(b)
else:
    print(c)

Trace through a = 7, b = 12, c = 12:

  • a >= b? 7 >= 12 is False. First branch skipped.
  • b >= c? 12 >= 12 is True. Prints 12.

For a = 12, b = 12, c = 5:

  • a >= b? 12 >= 12 is True.
  • a >= c? 12 >= 5 is True.
  • Both true, so print a, 12. Done.

Common mistakes.

  • Using > instead of >=. With three-way ties, no branch matches and the program falls through to print(c) even when a held the max. The value is right, the logic muddled.

Done?

The next chapter, Boolean logic in depth, digs into what and, or, and not actually do. After that come loops — the other way to make a program do something different each run.