Mini-Project: FizzBuzz
Before you start
#What you'll build
A program that prints the numbers 1 to 100, but with a twist:
- For multiples of 3, print "Fizz" instead of the number.
- For multiples of 5, print "Buzz" instead.
- For multiples of both 3 and 5 (i.e., multiples of 15), print "FizzBuzz".
Sample interaction
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
...
#What you'll learn
After this lesson, you will be able to:
- Walk through a range of numbers with for ... in range(...)
- Use the modulo operator % to ask whether one number divides another
- Order if/elif/else branches so the most specific case is tested first
#Step 1: Print 1 to 15
for loop and range(). Remember that range(1, 16) produces 1, 2, 3, ... 15. The upper bound is excluded.for n in range(1, 16):
print(n)range(1, 16) and not range(1, 15). The second argument is exclusive. Off-by-one errors here are a very common beginner mistake in Python.What does 10 % 3 evaluate to?
How many numbers does `range(1, 16)` produce?
#Step 2: Detect multiples of 15
%) returns the remainder after division. If n % 15 == 0, then 15 divides n evenly, meaning n is a multiple of 15.for n in range(1, 31):
if n % 15 == 0:
print("FizzBuzz")
else:
print(n)15 and 30 should print FizzBuzz; everything else prints the number. Verify before moving on.
What does this print for n = 15? if n % 3 == 0: print("Fizz") elif n % 5 == 0: print("Buzz") else: print(n)
if/elif chain stops at the first match: as soon as n % 3 == 0 matches, Python runs that branch and skips the rest. The % 5 check never even happens. This is exactly why FizzBuzz needs the % 15 check FIRST — without it, every multiple of 15 silently becomes "Fizz" instead of "FizzBuzz". One missing branch ruins the whole program in a way no error catches.#Step 3: Add Fizz (3) and Buzz (5)
Now extend with two more branches. Multiples of 3 print "Fizz", multiples of 5 print "Buzz". Order matters here.
for n in range(1, 16):
if n % 15 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n)n % 3 first, then 15 would print "Fizz" because 15 is also a multiple of 3, and the elif chain stops at the first match, before the "FizzBuzz" branch ever runs.What happens if you check `n % 3 == 0` before `n % 15 == 0`?
#Step 4: Scale to 100
range().for n in range(1, 101):
if n % 15 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n)That's it. The entire FizzBuzz problem fits in 9 lines. The output is 100 lines long, with "FizzBuzz" at 15, 30, 45, 60, 75, 90.
What does this print for n = 30? print(n % 3 == 0 and n % 5 == 0)
30 % 3 is 0 (30 divides evenly by 3), and 30 % 5 is also 0. Both comparisons 0 == 0 are True, and True and True is True. This expression is the LOGICAL equivalent of n % 15 == 0 — both detect multiples of 15. The and version is sometimes clearer because it explicitly shows both rules; the % 15 version is shorter because it's one test. Both are correct.#Now build it yourself
FizzBuzz from 1 to 100
Write a function `fizzbuzz(limit)` that prints each number from 1 up to and including `limit` on its own line. Replace multiples of 3 with 'Fizz', multiples of 5 with 'Buzz', and multiples of both (i.e., 15) with 'FizzBuzz'. Use only one loop and a single if/elif/else chain; the starter calls `fizzbuzz(100)`.
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
... (continues to 100)#Now make it your own
Try one of these variations:
- FizzBuzz string concat — solve it without the FizzBuzz branch. Build the output by concatenating "Fizz" if divisible by 3 and "Buzz" if divisible by 5; if the result is empty, print the number. (Pure FizzBuzz pattern — no
% 15check.) - Custom divisors — ask the user for two divisors and two words at runtime. "Give me a number and a word — I'll print that word for every multiple." Then run FizzBuzz with their inputs. (In this browser, put the answers on separate lines in the "Input for this program" box before you press Run.)
- Collect, don't print — build a LIST of the results instead of printing immediately. Print the whole list at the end with
print(", ".join(results)).