FizzBuzz
The task
Problem
Print the numbers 1 through 20. Replace multiples of 3 with Fizz, multiples of 5 with Buzz, and multiples of both with FizzBuzz.
How to think about it
Approach
Check divisibility by 15 first so a number divisible by both 3 and 5 is not handled by an earlier branch.
Complete Python solution
Switch languages without losing your place.
for number in range(1, 21):
if number % 15 == 0:
print("FizzBuzz")
elif number % 3 == 0:
print("Fizz")
elif number % 5 == 0:
print("Buzz")
else:
print(number)Verified output
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz