Lab · School of Computer Science & Technology

Lab 2 — Control Flow (mini FizzBuzz)

⏱️ ~7 minutes · Use Python. Solutions in solutions.html.

Goal

Combine a loop, the % operator, and an if/elif/else chain — the three ingredients of almost every real program.

Tasks

  1. Loop over the numbers 1 through 20 (hint: range(1, 21)).

  2. For each number n, print:

    • "Fizz" if n is a multiple of 3 (hint: n % 3 == 0),
    • otherwise the number itself.
  3. Now extend it: also print "Buzz" when n is a multiple of 5, and "FizzBuzz" when it's a multiple of both 3 and 5. ⚠️ Order matters — check the "both" case first. Why?

Stretch (optional)

What you're practicing

Loops, modulo (%), and branching with if/elif/else — and the classic ordering bug where the "both" condition must come before the single ones.

← Lab 1Lab 3 →