Lab · School of Computer Science & Technology
Lab 2 — Control Flow (mini FizzBuzz)
Goal
Combine a loop, the %
operator, and an if/elif/else
chain — the three ingredients of almost every real program.
Tasks
Loop over the numbers 1 through 20 (hint:
range(1, 21)).For each number
n, print:"Fizz"ifnis a multiple of 3 (hint:n % 3 == 0),- otherwise the number itself.
Now extend it: also print
"Buzz"whennis 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)
- Add a
breakthat stops the loop the first time you print"FizzBuzz". - Rewrite the 1–20 loop as a
whileloop instead of aforloop. Which reads more clearly?
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.