DEV Community

Shahrouz Nikseresht
Shahrouz Nikseresht

Posted on

Day 24: Python Countdown with Boom – Reverse Loop Printing "Boom" on Multiples of 3

Welcome to Day 24 of the #80DaysOfChallenges journey! Today's beginner exercise involves crafting a countdown function that swaps numbers divisible by 3 with "Boom", using a reverse loop to go from n down to 1. This builds familiarity with range() in reverse, modulo checks, and basic print logic, handy for any sequenced output or game-like patterns. If you're getting the hang of loops or need a quick drill on conditions, this "Python countdown boom" setup illustrates how to control flow without overcomplicating things.


💡 Key Takeaways from Day 24: Countdown Boom Function

This challenge puts together a function that counts backward, replacing multiples of 3 with "Boom" along the way. It's a clean demo of loop mechanics: set a range, check a condition, print accordingly. We'll break it down into function structure with print output, reverse iteration via range(), and modulo-based conditionals in action.

1. Function Design: Direct Print Output

The countdown_and_boom function takes an integer n and prints the sequence without returning anything. Its signature is practical:

def countdown_and_boom(n: int) -> None:
    """
    Count down from n to 1, printing "Boom" instead of numbers divisible by 3.
    """
Enter fullscreen mode Exit fullscreen mode

It kicks off with a header:

print(f"\n--- Starting countdown from {n} ---")
Enter fullscreen mode Exit fullscreen mode

This frames the output nicely. The function handles everything internally, loop, checks, prints, making it a standalone routine. Wrap it up with a footer: print("--- Countdown finished ---\n"). It's ideal for console apps or scripts where you want immediate, formatted results without extra handling.

2. Reverse Loop: Using range() with Step

The loop counts down using range():

# Use a for loop with range() to count down
# range(start, stop, step) -> stop is exclusive, so it stops right before 0
for i in range(n, 0, -1):
Enter fullscreen mode Exit fullscreen mode

Here, n is the start, 0 excludes 0, and -1 steps backward. It's a reliable way to iterate in reverse without lists or while loops. This setup shines in countdowns, timers, or any decreasing sequence, keeping the code concise and readable.

3. Conditional Check: Modulo for "Boom"

Inside the loop, modulo decides what to print:

# Check divisibility by 3 using the modulo (%) operator
if i % 3 == 0:
    print("Boom")     # Print "Boom" for multiples of 3
else:
    print(i)          # Otherwise, print the number itself
Enter fullscreen mode Exit fullscreen mode

% 3 == 0 flags multiples cleanly. For n=7, it prints: 7, 6 (Boom), 5, 4, 3 (Boom), 2, 1. The example call countdown_and_boom(7) shows it in full, with headers for context. It's a pattern that adapts easily, change the divisor or output for fizz-buzz variants or custom filters.


🎯 Summary and Reflections

This countdown with "Boom" ties loops and conditions into a fun, visual exercise. It drove home for me:

  • Range flexibility: The step param makes reverse easy, no extra work needed.
  • Modulo utility: A quick check for patterns in numbers.
  • Print-focused functions: Great for interactive or logging tasks.

The neat part was seeing how a small tweak like "Boom" makes loops feel dynamic. Ideas for more: Add delays with time.sleep() or handle n <= 0 gracefully.

Advanced Alternatives: Go functional with a list comp: [ "Boom" if i % 3 == 0 else i for i in range(n, 0, -1) ], then print them. Or use while: while n > 0: ... n -= 1. How do you spice up basic loops? Comment your ideas!


🚀 Next Steps and Resources

Day 24 added a twist to standard loops, gearing up for more patterned challenges. Following #80DaysOfChallenges? Tried a different divisor? Share your version!

Top comments (0)