Editor

FizzBuzz

Loops · Intermediate · 75 XP · about 8 min

The classic programming challenge! Write a function called fizz_buzz(n) that returns a list of strings from 1 to n with the following rules:

  • For multiples of 3, use "Fizz" instead of the number
  • For multiples of 5, use "Buzz" instead of the number
  • For multiples of both 3 and 5, use "FizzBuzz"
  • Otherwise, use the number as a string

Example

fizz_buzz(5)   # Returns ["1", "2", "Fizz", "4", "Buzz"]
fizz_buzz(15)  # Returns ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8",
               #          "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]

Requirements

  • Return a list of strings
  • Numbers that are multiples of both 3 and 5 should be "FizzBuzz"
  • Handle n = 1 correctly
def fizz_buzz(n):
# Your code here
pass
# Test your function
print(fizz_buzz(15))

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.