Easy ยท ages 9+ ยท if / elif / else ยท remainder ยท loops
FizzBuzz in Python.
Count from 1 to 20. For multiples of 3 print Fizz, for multiples of 5 print Buzz, and for both print FizzBuzz.
The program
Press Run to see what it prints. Change anything you like, then run it again.
for n in range(1, 21):
if n % 15 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n)1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz
How it works
- FizzBuzz is a famous warm-up problem. The order of the checks matters: the test for 15 comes first, because multiples of 15 are multiples of both 3 and 5.
elifmeans 'else, if': the next test only runs if the earlier ones failed.- Try moving the
n % 15test to the end and see what changes.
Try this
- Change Fizz and Buzz to your own words.
- Use 4 and 7 instead of 3 and 5.
- Count up to 100.
Stuck on an error? Read how to debug Python.
Similar ideas
More programs
FAQ
FizzBuzz: questions
What does the fizzbuzz program do?
Count from 1 to 20. For multiples of 3 print Fizz, for multiples of 5 print Buzz, and for both print FizzBuzz. It prints: 1 / 2 / Fizz โฆ
Which Python ideas does it use?
if / elif / else, remainder, loops. Easy level, suitable from about age 9.
How do I run it?
Press Run on this page. To run it on your own computer, install Python 3 and save the code in a file ending in .py. It prints the same thing.
What should I try next?
Change Fizz and Buzz to your own words. Use 4 and 7 instead of 3 and 5. Count up to 100.
Start a conversation
Want a teacher to guide the learning?
Tell us the learner's age and what they can already do, and we will suggest a starting point. A parent or guardian can make the enquiry.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay