Book a free demo

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

  1. 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.
  2. elif means 'else, if': the next test only runs if the earlier ones failed.
  3. Try moving the n % 15 test to the end and see what changes.
See the Python path โ†’

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

All 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

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

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.

All free games โ†’
Chat on WhatsApp
Esc

Try