Skip to content
scipython.com

Q2.5.3Fizzbuzz

The game of "Fizzbuzz" involves counting, but replacing numbers divisible by 3 with the word 'Fizz', those divisible by 5 with 'Buzz', and those divisible by both 3 and 5 with 'FizzBuzz'. Write a program to play this game, counting up to 100.

Show solution

The following code produces the first 100 "fizzbuzz" numbers.

nmax = 100
for n in range(1, nmax+1):
    message = ''
    if not n % 3:
        message = 'fizz'
    if not n % 5:
        message += 'buzz'
    print(message or n)

Note that if n is not divisible by either 3 or 5, message will be the empty string which evaluates to False in the logical expression message or n, so n is printed instead.