Solution to FizzBuzz in Python
In the last guide, I gave you the project requirements for implementing the FizzBuzz algorithm into your python program. And in this guide I'm going to walk through my own personal solution.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now I will mention before I get started that there are a number of ways to implement FizzBuzz. And so what we're going to do is going to be my personal solution. However, if you implemented a version that looks slightly different and it still works and then that is also perfectly fine.

I'm going to place each one of these requirements up here into our comment just so we can cross them off the list.

large

So the very first thing that we know we need to implement is a function and we want to call it fizz_buzz. So I am going to say def and then I'll just call it fizz_buzz and I mentioned that I wanted to have the arbitrary Max. And so I'm going to call that argument max_num and then I can drop into the function body. I'm gonna cross this off the list because we've created a function

large

and we also know that we want to call this function with something like this syntax here. So I want to pass in the number 100 and this should give me exactly what we are looking for up here in the project requirements body.

fizz_buzz(100)

So the next thing I'm going to implement is we want to loop through the entire set of elements all the way up until whatever that Max is so I'm gonna say for num in and I'm going to just create a range say for num in range starting at 1 and then going to the max num plus 1.

for num in rage(1, max_num + 1)

And the reason why we're doing this is, remember that whenever you pass in a range Python goes up and it treats that as the upper boundary. So if we were to give max_num a hundred then this would actually only go from 1 to ninety-nine. So I'm passing in max_num + 1 and this will give us 100. So this is going to be for in the loop. Then from there, I am going to implement my conditional, so right now we already have our loop done so we're moving ahead rather nicely.

large

Now we need to implement the condition, so we need to check for a number of items. Specifically, we need three conditionals because we know that we need to check for multiples of 3 multiples of 5 and then multiples of 3 and 5. This is one spot of fizz buzz that can trip up a number of developers. So if this part messed you up or you ran into some odd behavior right here do not worry what we are going to do is walk through the solution for that.

The tricky part is if you follow the order. So if I check for a condition of 3 and then I checked for a condition of 5. The problem with that is I will not be able to check for the conditions of 3 and 5 are the conditions of 15. Because technically when you say that we would have passed in the number 15.

So say that num equals 15 and that's where it's at in the loop. If we had a condition here that said if num % 3 is equal to zero.

if num % 3 == 0:

Well, in the case of 15. 15 modulo 3 is equal to zero. And so even though we were looking for 15 to be a perfect match or I should say 3 and 5 to be a perfect match this would actually not work and we'd get some weird behavior because this condition would be true and then it would print out fizz when we actually wanted it to print out fizz buzz.

So the solution to this is actually a little bit trickier. And so we're going to check for the last condition first so we're going to check to see if an element is a multiple of 3 and 5 and this is going to force us to use a compound conditional so I'm gonna say if the number modulo 3 is equal to zero and num modulo 5 is equal to 0, then I want you to print out fizz buzz.

dif fizz_buzz(max_num):
  for num in range(1, max_num +1):
    if num % 3 == 0 and num % 5 == 0:
      print('FizzBuzz')

fizz_buzz(100)

And so this is the behavior we're looking for and this is really the trickiest part of the entire program is making sure you get that part right.

So now with that in place, I can then say elif so now let's go and let's find those other conditions so now if it is simply divisible by 3 then we are going to print Fiz. And then lastly and this still has to be an elif because we still are checking for a condition. So now if it is divisible by 5 then we're just going to print out buzz, so this is our third condition.

elif num % 5 ==0:

Then we have to have a final else. So I'm going to say else and then this is where we simply print out the number itself. So here I can say print and then num.

else:
  print(num)

And let's see what our result is right now. So I'm going to come up to the top make sure so we have conditionals we have our math operators everything is in place so we have completed each one of the tasks.

large

But now let's see if it's actually working. So if I run this let's come down and let's actually count it from the beginning. So we have one two and then three. So right now we have three being fizz that is correct.

large

And then if we go to the five this is Buzz Fizz is at 6 so 6 is a something that three is a multiple of.

large

So that is correct and then 7, 8, 9 we have Fizz again Buzz at 10 that's perfect coming down at 15 and we have FizzBuzz. So if we come down all the way to 30 then we have FizzBuzz once again.

large

And so all of this is working perfectly.

So if you went through this and you have code that looks something like mine and you have a result that is also giving the same type of behavior then great job! You successfully went through and built FizzBuzz.

This is one of the most common programming interview questions out there so it is a good one to know. And one of the most important key takeaways is to ensure that you are always careful with your order of conditionals because if you get that mixed up then it can lead to some confusing behavior. So very nice work if you went through that and you can now continue on with the rest of the course.

Code

"""
Write a program that prints the numbers from 1 to 100.
But for multiples of three print “Fizz” instead of the
number and for the multiples of five print “Buzz”. For
numbers which are multiples of both three and five print
“FizzBuzz”.
"""

def fizz_buzz(max_num):
  for num in range(1, max_num + 1):
    if num % 3 == 0 and num % 5 == 0:
      print('FizzBuzz')
    elif num % 5 == 0:
      print('Buzz')
    elif num % 3 == 0:
      print('Fizz')
    else:
      print(num)


fizz_buzz(100)