FizzBuzz in Python

Count from 1 to n, replacing some numbers with words.

What to build

The classic counting game.

Requirements

  • Print Fizz for multiples of 3, and Buzz for multiples of 5.
  • Print FizzBuzz for multiples of both, and the number otherwise.

Input

One whole number n.

Output

n lines.

Constraints

n is from 1 to 100.

Example

Input:

5

Output:

1
2
Fizz
4
Buzz

Why it works

Only the first true branch of a chain runs, so the most specific test has to come first.

Examples

Example 1 · Input
5
Expected output
1
2
Fizz
4
Buzz
Example 2 · Input
1
Expected output
1

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Test for multiples of 15 first.

Hint 2

Use range(1, n + 1).

Solution

Show one correct solution (try it yourself first)
n = int(input())
for i in range(1, n + 1):
    if i % 15 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

Lesson: What comes next →