1
00:00:00,480 --> 00:00:02,910
All right, this is the final debugging exercise

2
00:00:02,910 --> 00:00:07,910
for today, and it is to debug our FizzBuzz program.

3
00:00:08,640 --> 00:00:10,290
Read the code carefully,

4
00:00:10,290 --> 00:00:13,650
see if you can spot any problems first time.

5
00:00:13,650 --> 00:00:17,220
If you can't, run the code and see what errors you get

6
00:00:17,220 --> 00:00:20,340
or submit the code and see which tests don't pass

7
00:00:20,340 --> 00:00:21,390
in order to give you a hint

8
00:00:21,390 --> 00:00:24,600
as to where the program might be going wrong.

9
00:00:24,600 --> 00:00:27,060
Now, if you can't remember how the FizzBuzz program

10
00:00:27,060 --> 00:00:28,470
is supposed to work,

11
00:00:28,470 --> 00:00:30,930
take a look inside the Description box

12
00:00:30,930 --> 00:00:33,660
where we've got a detailed description to remind you

13
00:00:33,660 --> 00:00:35,460
of how it's meant to work.

14
00:00:35,460 --> 00:00:38,310
Remember that in this debugging problem,

15
00:00:38,310 --> 00:00:41,310
I've actually put in quite a few bugs.

16
00:00:41,310 --> 00:00:44,550
You could call it a bug fest,

17
00:00:44,550 --> 00:00:48,480
so have fun squishing bugs, and see if you can figure out

18
00:00:48,480 --> 00:00:51,003
and find all the bugs and fix the code.

19
00:00:54,000 --> 00:00:55,380
Okay, I was a little bit mean

20
00:00:55,380 --> 00:00:57,600
and I maybe put in quite a few bugs in there

21
00:00:57,600 --> 00:00:58,830
for you to find,

22
00:00:58,830 --> 00:01:02,130
but it's good getting debugging practice,

23
00:01:02,130 --> 00:01:04,620
and this is the last debugging practice of the day.

24
00:01:04,620 --> 00:01:07,950
So take a look at line 4.

25
00:01:07,950 --> 00:01:10,920
The original code, we had:

26
00:01:10,920 --> 00:01:13,980
if number % 3 == 0

27
00:01:13,980 --> 00:01:16,920
or number % 5 == 0,

28
00:01:16,920 --> 00:01:18,300
and what this is going to do

29
00:01:18,300 --> 00:01:20,280
is it's going to check when the number

30
00:01:20,280 --> 00:01:23,610
is divisible by 3 or divisible by 5.

31
00:01:23,610 --> 00:01:24,780
It's not going to check

32
00:01:24,780 --> 00:01:27,750
when both of these conditions are True.

33
00:01:27,750 --> 00:01:29,370
And in order to do that,

34
00:01:29,370 --> 00:01:31,050
we have to fix this line of code

35
00:01:31,050 --> 00:01:33,480
and change "or" to "and",

36
00:01:33,480 --> 00:01:36,440
that way we know when we need to print "FizzBuzz".

37
00:01:37,440 --> 00:01:39,750
The next two bugs are pretty much related.

38
00:01:39,750 --> 00:01:41,310
I had to put them both in,

39
00:01:41,310 --> 00:01:43,860
otherwise it would look a bit suspicious.

40
00:01:43,860 --> 00:01:47,790
And the problem here is we have two if statements:

41
00:01:47,790 --> 00:01:52,790
if number % 3 == 0 and if number % 5 == 0.

42
00:01:53,700 --> 00:01:57,150
Now the problem with this is not only is it inefficient,

43
00:01:57,150 --> 00:02:01,260
it means that even if the first line, line 6, was True,

44
00:02:01,260 --> 00:02:05,100
it's going to continue checking every single if statement.

45
00:02:05,100 --> 00:02:07,260
Whereas if we have an elif statement,

46
00:02:07,260 --> 00:02:11,730
then at any point, it could end the if, elif, else loop

47
00:02:11,730 --> 00:02:13,530
once it finds one of them to be true,

48
00:02:13,530 --> 00:02:15,450
so that's an early exit option

49
00:02:15,450 --> 00:02:17,580
and it's more efficient in most cases.

50
00:02:17,580 --> 00:02:19,890
Now, there's another crucial downfall

51
00:02:19,890 --> 00:02:23,520
to just using if statements in this code

52
00:02:23,520 --> 00:02:28,520
in the sense that if it's meant to be FizzBuzz,

53
00:02:28,620 --> 00:02:30,030
it should print FizzBuzz,

54
00:02:30,030 --> 00:02:31,950
it should not print Fizz

55
00:02:31,950 --> 00:02:35,970
because it's divisible by 3 and Buzz and FizzBuzz.

56
00:02:35,970 --> 00:02:38,310
That will mess up our structure

57
00:02:38,310 --> 00:02:40,680
of our answer pretty dramatically.

58
00:02:40,680 --> 00:02:44,430
So in this case, it should only print Fizz

59
00:02:44,430 --> 00:02:48,540
when the first condition on line 6 is False,

60
00:02:48,540 --> 00:02:51,930
and then it falls into the next condition to check,

61
00:02:51,930 --> 00:02:54,720
well, if it's not divisible by 3 and 5,

62
00:02:54,720 --> 00:02:57,540
is it divisible by 3 alone?

63
00:02:57,540 --> 00:02:59,370
And then if that's False as well,

64
00:02:59,370 --> 00:03:00,450
then we go and check, well

65
00:03:00,450 --> 00:03:03,390
is it divisible by 5 alone,

66
00:03:03,390 --> 00:03:05,430
in which case it should print Buzz.

67
00:03:05,430 --> 00:03:08,220
The flow of a conditional statement

68
00:03:08,220 --> 00:03:11,070
is very important thing to wrap your head around,

69
00:03:11,070 --> 00:03:14,940
and this is a good example of where it is crucial

70
00:03:14,940 --> 00:03:16,803
to use elif instead of if.

71
00:03:18,300 --> 00:03:21,210
Now the final bug is just a bit of a naughty one from me.

72
00:03:21,210 --> 00:03:23,370
Instead of printing out a number,

73
00:03:23,370 --> 00:03:26,040
if we wrap some square brackets around that number,

74
00:03:26,040 --> 00:03:29,730
we print out a list containing our number,

75
00:03:29,730 --> 00:03:31,950
which is not what we are checking against,

76
00:03:31,950 --> 00:03:34,230
that's not what we want.

77
00:03:34,230 --> 00:03:36,030
In order to fix this, it's pretty simple,

78
00:03:36,030 --> 00:03:38,010
just remove the square brackets [ ].

79
00:03:38,010 --> 00:03:40,020
Now, a lot of this involves being sensitive

80
00:03:40,020 --> 00:03:42,810
to how code looks, and the more code you read,

81
00:03:42,810 --> 00:03:44,610
the better you get at debugging,

82
00:03:44,610 --> 00:03:47,460
it's not one of those skills that we're born with for sure.

83
00:03:47,460 --> 00:03:51,870
And if Hollywood movies portray hackers as typing away,

84
00:03:51,870 --> 00:03:53,040
that is not the truth,

85
00:03:53,040 --> 00:03:54,750
most of the time we are debugging,

86
00:03:54,750 --> 00:03:56,760
we are staring deep into space

87
00:03:56,760 --> 00:03:58,830
and trying to figure out what it is

88
00:03:58,830 --> 00:04:00,450
that we've written that is causing

89
00:04:00,450 --> 00:04:02,970
this catastrophic failure of our code.

90
00:04:02,970 --> 00:04:04,500
So don't be disheartened,

91
00:04:04,500 --> 00:04:07,710
this is an important skill to hone and to build,

92
00:04:07,710 --> 00:04:10,440
and this is just one step towards that.

93
00:04:10,440 --> 00:04:11,673
Failure is your friend.

