All right, this is the final debugging exercise
for today, and it is to debug our FizzBuzz program.
Read the code carefully,
see if you can spot any problems first time.
If you can't, run the code and see what errors you get
or submit the code and see which tests don't pass
in order to give you a hint
as to where the program might be going wrong.
Now, if you can't remember how the FizzBuzz program
is supposed to work,
take a look inside the Description box
where we've got a detailed description to remind you
of how it's meant to work.
Remember that in this debugging problem,
I've actually put in quite a few bugs.
You could call it a bug fest,
so have fun squishing bugs, and see if you can figure out
and find all the bugs and fix the code.
Okay, I was a little bit mean
and I maybe put in quite a few bugs in there
for you to find,
but it's good getting debugging practice,
and this is the last debugging practice of the day.
So take a look at line 4.
The original code, we had:
if number % 3 == 0
or number % 5 == 0,
and what this is going to do
is it's going to check when the number
is divisible by 3 or divisible by 5.
It's not going to check
when both of these conditions are True.
And in order to do that,
we have to fix this line of code
and change "or" to "and",
that way we know when we need to print "FizzBuzz".
The next two bugs are pretty much related.
I had to put them both in,
otherwise it would look a bit suspicious.
And the problem here is we have two if statements:
if number % 3 == 0 and if number % 5 == 0.
Now the problem with this is not only is it inefficient,
it means that even if the first line, line 6, was True,
it's going to continue checking every single if statement.
Whereas if we have an elif statement,
then at any point, it could end the if, elif, else loop
once it finds one of them to be true,
so that's an early exit option
and it's more efficient in most cases.
Now, there's another crucial downfall
to just using if statements in this code
in the sense that if it's meant to be FizzBuzz,
it should print FizzBuzz,
it should not print Fizz
because it's divisible by 3 and Buzz and FizzBuzz.
That will mess up our structure
of our answer pretty dramatically.
So in this case, it should only print Fizz
when the first condition on line 6 is False,
and then it falls into the next condition to check,
well, if it's not divisible by 3 and 5,
is it divisible by 3 alone?
And then if that's False as well,
then we go and check, well
is it divisible by 5 alone,
in which case it should print Buzz.
The flow of a conditional statement
is very important thing to wrap your head around,
and this is a good example of where it is crucial
to use elif instead of if.
Now the final bug is just a bit of a naughty one from me.
Instead of printing out a number,
if we wrap some square brackets around that number,
we print out a list containing our number,
which is not what we are checking against,
that's not what we want.
In order to fix this, it's pretty simple,
just remove the square brackets [ ].
Now, a lot of this involves being sensitive
to how code looks, and the more code you read,
the better you get at debugging,
it's not one of those skills that we're born with for sure.
And if Hollywood movies portray hackers as typing away,
that is not the truth,
most of the time we are debugging,
we are staring deep into space
and trying to figure out what it is
that we've written that is causing
this catastrophic failure of our code.
So don't be disheartened,
this is an important skill to hone and to build,
and this is just one step towards that.
Failure is your friend.