FizzBuzz Problem
Take a number and print "Fizz" if divisible by 3, "Buzz" if divisible by 5, and "FizzBuzz" if divisible by both.
IntermediatePhase 1 - Level 4: Logical Operators & Compound StatementsExample 2 of 10
fizzbuzz-problem.py
1# Take number2num = int(input("Enter a number: "))34# FizzBuzz logic5if num % 3 == 0 and num % 5 == 0:6 print("FizzBuzz")7elif num % 3 == 0:8 print("Fizz")9elif num % 5 == 0:10 print("Buzz")11else:12 print(num)
Output
Enter a number: 15 FizzBuzz Enter a number: 9 Fizz Enter a number: 10 Buzz Enter a number: 7 7
What's going on
Check divisibility conditions in correct order.
Key Concepts:
Check both conditions first (FizzBuzz)
Then check individual conditions (Fizz, Buzz)
Order matters: check both before individual
🔗Related Content
- 📖
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- 📝
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- 💻
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum