Fizz Buzz is a classic coding challenge based on a game played at school in Maths lessons.
Fizz Buzz is a game for two or more players. Take it in turns to count aloud from 1 to 50, but each time you are going to say a multiple of 3, replace it with the word “fizz”. For multiples of 5, say “buzz” and for numbers which are multiples of both 3 and 5, say “fizz, buzz”.
Have a go at writing some Python code to simulate this game now. Your version should just print out a number, or “fizz”, “buzz”, or “fizzbuzz” for each of the numbers from
1 - 50. Don’t worry whether you get it right. Just try. You will learn plenty even if you can’t get it to work, including understanding what doesn’t work!
Before I share a solution, check out this related problem, to help understand some of the issues you may come across when trying to implement Fizzbuzz in Python.
Python Grades Exercise
elsestatements, write a program asking the user to input their exam score out of 100. Based on the value entered, the program should output the following grades.
|49 or lower||E|
|80 or above||A|
Try it yourself if you couldn’t do it “with your eyes closed” (metaphorically).
A valiant fist attempt at this problem might result in something like this:
result = int(input("Please enter your percentage grade: ")) if result < 50: print("Grade U") elif result >= 50: print("Grade D") elif result >= 60: print("Grade C") elif result >= 70: print("Grade B") else: print("Grade A")
Can you see what is wrong with that solution? You may well not have fallen into the problem illustrated by this example, and would probably be less likely to if I had presented the scores in a different order, such as this:
|80 or above||A|
|49 or lower||U|
Another perfectly valid way of handing the potential issue is to be very specific about the bounds for the grades by including both end points. E.g.
if result >= 70 and result < 80 ... (Notice that
result needs repeating in that condition – a quite common error would be
if result >= 70 and < 80 ... Not correct, as
< is a binary operator, and needs two things to compare!).
By the way, here’s a working solution to the grades exercise for reference.
result = int(input("Please enter your percentage grade: ")) if result > 80: print("Grade A") elif result >= 70: print("Grade B") elif result >= 60: print("Grade C") elif result >= 50: print("Grade D") else: print("Grade U")
There are a few points that can be taken from this example.
One is that there are often multiple ways to achieve the same goal, particularity with logical expressions. But the main point for the purposes of this lesson, is the idea of premature inclusion.
What do I mean by premature inclusion? Well, in the first solution, a grade of 100 would meet the condition of being
>= 50, so would be accepted as a D grade and the algorithm would finish, printing the wrong result.
Python Fizz Buzz Solution
In the context of the Fizzbuzz coding exercise, the same problem can arise as that illustrated above. People often test for “fizzness” and “buzzness” before checking for “fizzbuzzness”, which means a result is prematurely accepted on the basis of a single condition, when in fact both conditions hold.
Here’s an example of a version of Fizzbuzz in Python which contains this error:
for i in range(1, 51): if i % 3 == 0: print("fizz") elif i % 5 == 0: print("buzz") elif i % 3 == 0 and i % 5 == 0: print("fizzbuzz") else: print(i)
By the way, if you are not familiar with the modulo operator, you can quickly master it using this pack which I put together. The modulo operator is key to the Fizzbuzz problem, as well and many other important algorithms.
OK, so we are close to a working solution for Fizzbuzz in Python. I’ve hidden my solution to avoid spoilers. Once you have had a really good attempt yourself, click “show solution” to see how I solved the challenge.
for i in range(1, 51): if i % 3 == 0 and i % 5 == 0: print("fizzbuzz") elif i % 5 == 0: print("buzz") elif i % 3 == 0: print("fizz") else: print(i)
This lesson has explored the famous “Fizzbuzz” coding interview problem, along with some of the algorithmic challenges it presents. I hope you found it interesting and helpful.