Android & iPhone apps are coming soon. Log in for a plan and reminders made for you →

Coding basics · 7 of 20

FizzBuzz

For each number from 1 to n, add "Fizz" if it's divisible by 3, "Buzz" if by 5, "FizzBuzz" if by both, and otherwise the number itself as text. Return the list.

Jump to the editor ↓

What you need

Put an if inside a loop to decide something for every value. str() turns a number into text.

for i in range(1, 4):
    if i % 2 == 0:
        words.append("even")
    else:
        words.append(str(i))

Examples

Input n = 5

Output ["1", "2", "Fizz", "4", "Buzz"]

Input n = 15

Output […, "14", "FizzBuzz"]

15 is divisible by both 3 and 5.

Solved it? The same idea on LeetCode: try it there ↗

Your code

Tab indents · ⌘/Ctrl+Enter runs · Esc leaves the editor

Stuck?