In this coding exercise
you are going to write a program
that is often asked for at coding interviews,
so if you are looking for a job, take note.
Very often the examiner or the interviewer might ask you
write a program to recreate the FizzBuzz game.
Now, if you've never played the FizzBuzz game
it's pretty simple,
let's say you have five friends,
you stand in a circle
and each friend calls out the next number starting from 1.
Now, however, the game starts when you come across a number
that is divisible by 3 then instead you should say Fizz.
And if you come across a number that's divisible by 5
then instead of saying the number, you should say Buzz.
And if the number is divisible
by both 3 and 5, for example, 15
then instead of saying the number, you say, FizzBuzz.
This is a math game that has tormented many of the children
on a long road trip by their parents, including myself.
But in this case, we're going to convert it
into code so that it can do it for us.
Your program is going to print each number
from 1 to 100 in turn, so starting from 1, 2,
but when it comes across the number
that's divisible by 3,
it should print Fizz, divisible by 5 Buzz,
and if divisible by both, it's going to print FizzBuzz.
Take a look inside the Description pane
and see how it might look.
And I only go up to 15, but your goal is to go up to 100
and make sure that you are using code to do this
instead of just working it out.
And remember that your answer should start from 1
and print up to and including 100.
Now, 100 might not be 100 might be Fizz, might be Buzz,
it might be FizzBuzz,
you're going to have to use your code to work it out
and each number of text should be printed
on a separate line.
Those are the rules.
Now have a go and see if you can complete this challenge.
First thing I'm going to do is to set a target
and we're setting our target to 100.
And then I'm going to use that target to create a range.
Now remember, as you saw in the previous coding exercise,
the range() function takes a starting number
and an ending number, but it doesn't include
that ending number.
So we have to add one to our target in order
to create a range up to 100.
Now, once we've got our range,
it's going to increment through the range by 1
because we haven't specified otherwise,
and then we're going to loop through each of the numbers
in our range from 1 to 100.
Now, the first thing we're going to check
is if the number that we're currently looping through
can be cleanly divided by 3 and cleanly divided by 5,
then we're going to print FizzBuzz.
The order matters,
remember, an if statement starts checking from the top
and the elif's come if the first ones are false.
So if we first checked for,
if the number is just cleanly divisible by 3
and we print Fizz
then we're going to miss the situation
where it's also divisible by 5.
This was the important thing to realize
in the logic of this program
and maybe you worked it out by trying some things out
or by realizing it in the beginning
but this is something that you might have missed
and you might need to go back and check.
Now, the subsequent parts are pretty simple.
If the first condition is not met,
so it's not a number that can be divided by 3
and 5 at the same time, then we're going to check,
well, is it divisible by three cleanly?
In which case we'll print Fizz.
Otherwise, is it divisible by 5 cleanly,
in which we'll print Buzz
and otherwise, if it doesn't divide by any of those numbers
and doesn't meet any of those conditions,
we just print the number.
And because we're looping through the numbers one by one
we're going to get these printed out in sequence one by one
and checking for each of these criterias.