Python Loops
💡
Exercise 21

Fizz Buzz 20 XP Medium

Ctrl+Enter Run Ctrl+S Save

Fizz Buzz is one of the most famous coding challenges in the world! It's used in job interviews to test if someone actually knows how to code. 🏆

💡 The Rules:

  • Loop through numbers 1 to 15
  • If a number is divisible by 3, print Fizz
  • If a number is divisible by 5, print Buzz
  • If divisible by BOTH 3 and 5, print FizzBuzz
  • Otherwise, just print the number

The modulo operator % gives the remainder of division. If the remainder is 0, the number is evenly divisible:

print(6 % 3) # 0 → 6 IS divisible by 3 print(7 % 3) # 1 → 7 is NOT divisible by 3 print(15 % 5) # 0 → 15 IS divisible by 5 print(15 % 3) # 0 → 15 IS also divisible by 3!

⚠️ Common Mistake: You MUST check "divisible by both 3 AND 5" first! Why? Because 15 is divisible by 3, so if you check that first, you'll print "Fizz" and never reach "FizzBuzz".

Step by step for first few numbers:

  • 1 → not divisible by 3 or 5 → print 1
  • 2 → not divisible by 3 or 5 → print 2
  • 3 → divisible by 3 → print Fizz
  • 4 → nothing → print 4
  • 5 → divisible by 5 → print Buzz
  • 15 → divisible by BOTH → print FizzBuzz
📋 Instructions
Implement the FizzBuzz challenge for numbers 1-15: Expected output: ``` 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz ``` **Important**: Check divisibility by both 3 AND 5 first!
Use a for loop: for i in range(1, 16). Check i % 15 == 0 FIRST (FizzBuzz), then i % 3 == 0 (Fizz), then i % 5 == 0 (Buzz), else print(i).
⚠️ Try solving it yourself first — you'll learn more!
print(6 % 3)   # 0 → 6 IS divisible by 3
print(7 % 3)   # 1 → 7 is NOT divisible by 3
print(15 % 5)  # 0 → 15 IS divisible by 5
print(15 % 3)  # 0 → 15 IS also divisible by 3!
🧪 Test Cases
Input
Run your code
Expected
1 2 Fizz ...
Expected output (15 lines)
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.