25 XP

3. FizzBuzz

Beginner loops conditions

Write a function fizzbuzz(n) that takes a whole number n and returns a list of strings, one for each number from 1 to n, in order. For each number, the string is:

  • "FizzBuzz" if the number is a multiple of both 3 and 5,
  • "Fizz" if it is a multiple of 3 only,
  • "Buzz" if it is a multiple of 5 only,
  • otherwise the number itself, as a string: "7", not 7.

A multiple of 3 is a number that 3 divides with nothing left over: 3, 6, 9, 12 and so on. If n is 0, return an empty list [].

Example 1

Inputn = 5
Output["1", "2", "Fizz", "4", "Buzz"]
Explanation3 is a multiple of 3, 5 a multiple of 5.

Example 2

Inputn = 15
Output["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]
Explanation15 is a multiple of both 3 and 5.

Example 3

Inputn = 1
Output["1"]
Explanation1 is not a multiple of 3 or 5.

Constraints

  • n is a whole number from 0 to 100.

Submit also runs 3 more tests you can't see.

Practises For Loops (lesson 13).

Code
def fizzbuzz(n):
    """Build the FizzBuzz list for the numbers 1 to n.

    n: int, how many numbers (0 or more)
    Returns: list of str, one string for each number 1..n
    """
    # Write your code here
    pass