DEV Community

Shahrouz Nikseresht
Shahrouz Nikseresht

Posted on

Day 21: Turn-Based FizzBuzz Game – Player vs Machine in Python

Welcome to Day 21 of the #80DaysOfChallenges journey! Today’s beginner-to-intermediate challenge is a turn-based FizzBuzz game between you and the computer, built with while loops, string comparison, and alternating turns. This isn’t just another FizzBuzz script; it’s a real interactive game that practices user input, validation, and game flow. If you’re looking for a fun way to level up your loop and conditional skills, this “Python FizzBuzz game” is the perfect playground!


💡 Key Takeaways from Day 21: Interactive FizzBuzz Duel

The game starts at 1 and counts upward, alternating between you and the machine. Each turn requires the correct FizzBuzz output:

  • Fizz for multiples of 3
  • Buzz for multiples of 5
  • FizzBuzz for multiples of both
  • or the number itself

It even accepts shortcuts: F, B, FB. The game ends if you make a mistake or type q. Let’s break down the core pieces: answer generation, turn management, and interactive feedback.

1. Smart Answer Generation: The fizzbuzz_label Function

The fizzbuzz_label function returns all valid answers for a given number:

def fizzbuzz_label(n: int) -> list[str]:
    if n % 3 == 0 and n % 5 == 0:
        return ["FizzBuzz", "FB", "fizzbuzz", "fb"]
    elif n % 3 == 0:
        return ["Fizz", "F", "fizz", "f"]
    elif n % 5 == 0:
        return ["Buzz", "B", "buzz", "b"]
    else:
        return [str(n)]
Enter fullscreen mode Exit fullscreen mode

This function is flexible and user-friendly, accepting case variations and shortcuts. For example, at 15, it allows [FizzBuzz, FB, fizzbuzz, fb]. Using a list makes validation clean and lets players type f or FB and still win. A small function with big impact on gameplay.

2. Turn Management: while Loop + player_turn Toggle

The game runs in an infinite while True loop, switching turns with a boolean flip:

while True:
    expected = fizzbuzz_label(current_number)

    if player_turn:
        # Player's turn
    else:
        # Machine's turn
        print(f"Machine says: {expected[0]}")

    player_turn = not player_turn
    current_number += 1
    print_separator()
Enter fullscreen mode Exit fullscreen mode

The machine always says the correct answer (expected[0]), while you must match it. This alternating structure creates real back-and-forth tension and makes the loop feel alive.

3. Interactive Feedback: Emojis, Messages & Separators

Each turn comes with clear prompts and expressive feedback:

print(f"Your turn → Number: {current_number}")
user_input = input("Your answer: ").strip().lower()

if user_input in ("q", "quit"):
    print("You quit the game. Thanks for playing!")
    break

if user_input not in [ans.lower() for ans in expected]:
    print(f"Oops! Expected one of {expected}, but you said '{user_input}'.")
    print("Machine wins this round!")
    break
else:
    print("Nice! That’s correct!")
Enter fullscreen mode Exit fullscreen mode

Using .strip().lower() cleans input, and a list comprehension compares it safely. The print_separator() function adds visual breathing room:

print("\n" + "" * 45 + "\n")
Enter fullscreen mode Exit fullscreen mode

Finally, the game ends with a clean summary:

print(f"FINAL NUMBER REACHED: {current_number - 1}")
print("GAME OVER — THANKS FOR PLAYING!")
Enter fullscreen mode Exit fullscreen mode

🎯 Summary and Reflections

This FizzBuzz game proved that a simple concept can teach deep lessons in loops, user input, and UX design. It made me think about:

  • Input flexibility: Shortcuts and case-insensitivity = better player experience.
  • Game flow: while + break + boolean toggle = smooth turn logic.
  • Visual polish: Emojis and separators turn dry code into a real game.

The fun moment? When the machine calmly says the right answer while you fumble, real competition! For extensions, I’d add score tracking, max number limit, or two-player mode.

Advanced Alternatives: Use match-case for FizzBuzz logic, or add color with colorama. How do you build interactive games? Share your ideas below!


🚀 Next Steps and Resources

Day 21 pulled me into the world of interactive games and sharpened my loop and input skills. If you're on the #80DaysOfChallenges ride, did you mod the game? Add new shortcuts? Show your code!

Onward to Day 22 - ready for more algorithms!

Top comments (0)