← All coding practice

Coding Practice · Beginner · October 15, 2026 · 4 min read

Python Coding Exercise #39: FizzBuzz

Part of the series Python Practice Exercise — part 39

Before you start: Know input() and int() · Know the % operator · Know if, elif and else

The question

Please write a program which asks the user for an integer number. If the number is divisible by three, the program should print out Fizz. If the number is divisible by five, the program should print out Buzz. If the number is divisible by both three and five, the program should print out FizzBuzz.

Some examples of expected behaviour:

Number: 9
Fizz
Number: 7
Number: 20
Buzz
Number: 45
FizzBuzz

My Solution

number = int(input("Number: "))
if number % 3 == 0 and number % 5 == 0:
    print("FizzBuzz")
elif number % 3 == 0:
    print("Fizz")
elif number % 5 == 0:
    print("Buzz")

How My Solution Works

1. What “divisible by 3” means

A number is divisible by 3 when it divides by 3 with nothing left over. In Python, the % operator gives the remainder of a division, so “divisible by 3” can be checked with number % 3 == 0. (The % operator was used the same way in Exercise #12 and Exercise #21.)

Numbernumber % 3number % 5Divisible by
9043 only
712neither
20205 only
4500both 3 and 5

2. Both conditions together with and

The and operator joins two conditions, and the whole thing is true only when both are true. So number % 3 == 0 and number % 5 == 0 is true only for numbers divisible by 3 and by 5, like 45. (Its partner or was used in Exercise #37.)

3. Four outcomes, three branches

There are four possible outcomes: FizzBuzz, Fizz, Buzz, or nothing at all. The if / elif chain has three branches, and the “nothing” case is simply what happens when none of them match:

NumberFirst true branchPrinted
45number % 3 == 0 and number % 5 == 0FizzBuzz
9number % 3 == 0Fizz
20number % 5 == 0Buzz
7nonenothing

For 7 there is no else, so the program prints nothing, just like the sample output. An else: print() would print an empty line, which is not the same as nothing. (This was covered in Exercise #13.)

4. The order of the branches matters

The most important detail in this exercise is that the FizzBuzz check comes first. A number like 45 is divisible by 3, and it is also divisible by 5, so it matches every one of the three conditions. Python runs only the first branch that’s true, so the most specific condition has to be checked before the general ones.

If the checks are in the wrong order, FizzBuzz can never be printed:

number = int(input("Number: "))
if number % 3 == 0:
    print("Fizz")
elif number % 5 == 0:
    print("Buzz")
elif number % 3 == 0 and number % 5 == 0:
    print("FizzBuzz")

For 45, the first condition number % 3 == 0 is already true, so the program prints Fizz and stops. The FizzBuzz branch is never reached. The same happens for every multiple of 15, such as 15, 30 and 60, which all print Fizz instead of FizzBuzz. This is the same lesson as in Exercise #36: put the narrowest condition first.

University Solution

number = int(input("Number: "))

if number % 3 == 0 and number % 5 == 0:
    # This condition must be evaluated first, because if this is true,
    # also both of the following conditions are true
    print("FizzBuzz")
elif number % 3 == 0:
    print("Fizz")
elif number % 5 == 0:
    print("Buzz")

The University Solution is the same as My Solution. The only addition is the comment, which explains exactly the point above: the FizzBuzz condition must come first, because when it’s true, the two conditions after it are true as well.

Things to notice

Zero is divisible by everything. 0 % 3 and 0 % 5 are both 0, so an input of 0 prints FizzBuzz. That’s correct, because 0 is a multiple of every number.

The program only checks divisibility. The number 15 and the number 30 behave the same way, because both are multiples of 3 and 5. Testing a multiple of 15, such as 15 or 45, is the quickest way to check that the order of the branches is right.

Key takeaways

  • number % 3 == 0 checks whether a number is divisible by 3. The % operator gives the remainder.
  • and is true only when both conditions are true.
  • Python runs only the first true branch of an if / elif chain, so the narrowest condition has to come first. For FizzBuzz, that’s the “both” case.
  • A number divisible by both 3 and 5 is divisible by 15, so number % 15 == 0 works too.
  • With no else, a program prints nothing when no condition matches. An empty print() still prints a blank line.
  • Test with a multiple of 15 to be sure FizzBuzz works. Numbers like 9 and 20 can’t reveal a wrong order.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · conditionals · modulo · logical-operators · elif