Implement Fizz Buzz in Python

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Fizz Buzz is a popular programming challenge that is often used in coding interviews. It is a simple game that requires players to take turns counting up from 1, replacing any number that is divisible by 3 with the word "Fizz" and any number that is divisible by 5 with the word "Buzz". If a number is divisible by both 3 and 5, the player must say "FizzBuzz" instead of the number. In this challenge, we will be implementing Fizz Buzz using Python.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/arrays_strings("`Arrays Strings`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/catching_exceptions("`Catching Exceptions`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/ErrorandExceptionHandlingGroup -.-> python/custom_exceptions("`Custom Exceptions`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/arrays_strings -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/variables_data_types -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/strings -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/type_conversion -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/conditional_statements -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/for_loops -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/lists -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/tuples -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/function_definition -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/classes_objects -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/encapsulation -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/catching_exceptions -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/raising_exceptions -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/custom_exceptions -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} python/build_in_functions -.-> lab-268802{{"`Implement Fizz Buzz in Python`"}} end

Fizz Buzz

Problem

Implement Fizz Buzz using Python. Your function should take an integer n as input and return a list of strings representing the numbers from 1 to n, with the following modifications:

  • Multiples of 3 should be replaced with the string "Fizz"
  • Multiples of 5 should be replaced with the string "Buzz"
  • Multiples of both 3 and 5 should be replaced with the string "FizzBuzz"

Your function should also handle the following cases:

  • If the input is less than 1, raise an exception
  • If the input is not a valid integer, raise an exception

Requirements

To implement Fizz Buzz in Python, we need to follow these requirements:

  • Define a function that takes an integer n as input
  • Check if the input is a valid integer and raise an exception if it is not
  • Check if the input is less than 1 and raise an exception if it is
  • Create a list of strings representing the numbers from 1 to n, with the modifications described above
  • Return the list

Example Usage

assert fizz_buzz(15) == ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz']
try:
    fizz_buzz(0)
except ValueError:
    print("Invalid input")
try:
    fizz_buzz("hello")
except ValueError:
    print("Invalid input")
try:
    fizz_buzz(-5)
except ValueError:
    print("Invalid input")

Summary

In this challenge, we implemented Fizz Buzz using Python. We defined a function that takes an integer n as input and returns a list of strings representing the numbers from 1 to n, with modifications for multiples of 3, 5, and both. We also handled cases where the input is not a valid integer or is less than 1 by raising exceptions.

Other Algorithm Tutorials you may like