>_Code Club
← All exercises

python

FizzBuzz

FizzBuzz is a famous little puzzle. Count from 1 upwards, but:

  • if the number can be divided by 3, say Fizz
  • if it can be divided by 5, say Buzz
  • if it can be divided by both 3 and 5, say FizzBuzz
  • otherwise, just say the number

So: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, ...

"Can be divided by 3" means the remainder is 0: n % 3 == 0.

Your mission

  1. Write fizzbuzz(n) that returns the right word for the number n (as a string, so fizzbuzz(7) returns "7").
  2. Use a loop to print the answers from 1 to 30.

Watch out for the order of your ifs: what happens to 15?

Next exerciseGame: guess the number →
Loading the editor…