Day 5: The FizzBuzz Test

Print numbers from 1 to 20. For multiples of 3, print "Fizz". For multiples of 5, print "Buzz". For multiples of both 3 and 5, print "FizzBuzz". (Concepts: conditionals if/elif/else, modulo operator %)

Sample Code:

for i in range(1, 21):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

Students, Day 5 is a classic: FizzBuzz, teaching loops and multi-conditionals. We use a for loop: for i in range(1, 21) iterates i from 1 to 20 (range stops before 21). Each loop runs the indented block.

Inside: Nested if with elif (else if) and else. First check i % 3 == 0 and i % 5 == 0—and requires both true (for 15, 30, etc.). If yes, print "FizzBuzz". If not, elif i % 3 == 0 for "Fizz" (multiples of 3 only). Next elif for "Buzz" (5 only). Finally, else prints the number itself. Order matters—check both first to avoid wrong outputs.

Run it: 1,2,Fizz,4,Buzz,...15=FizzBuzz. In your time, change to 1-100 or add "Bang" for 7. Loops automate repetition; this preps for games or data processing. You're looping like pros!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top