1
00:00:00,060 --> 00:00:02,880
So we're onto our final debugging exercise.

2
00:00:02,970 --> 00:00:07,530
This is debugging Fizzbuzz. Fizzbuzz, if you remember,

3
00:00:07,530 --> 00:00:10,680
is a program that will print Fzzbuzz

4
00:00:11,100 --> 00:00:15,690
if a number is divisible by both three and five.

5
00:00:16,110 --> 00:00:17,370
It will print fizz

6
00:00:17,400 --> 00:00:21,120
if it's only divisible by three, and it will print buzz

7
00:00:21,180 --> 00:00:23,160
if it's only divisible by five.

8
00:00:23,550 --> 00:00:26,640
If it's not divisible by three or five,

9
00:00:26,970 --> 00:00:29,550
then it's just going to print the number.

10
00:00:30,150 --> 00:00:34,950
And our code is going to do this all the way from 1 to 100.

11
00:00:35,430 --> 00:00:40,230
So what should happen by the end of this is in our console when we hit run,

12
00:00:40,590 --> 00:00:45,590
it should start typing one and then it should type two and then three,

13
00:00:45,720 --> 00:00:49,710
it will type fizz. And four, it will type four. Five,

14
00:00:49,710 --> 00:00:51,810
it will type buzz and so on and so forth.

15
00:00:52,140 --> 00:00:55,590
You should remember this from the previous exercise where you actually had to

16
00:00:55,590 --> 00:00:58,830
write all this code. Now, as always,

17
00:00:59,040 --> 00:01:03,060
don't just paste the solution code in. That won't do anything.

18
00:01:03,330 --> 00:01:05,550
You need to work for it to gain knowledge.

19
00:01:06,120 --> 00:01:09,270
So now that you know what the program is supposed to do,

20
00:01:09,930 --> 00:01:14,640
what you are going to do is try to figure out all of the bugs in here and try to

21
00:01:14,670 --> 00:01:19,230
fix all the bugs so that the program runs as it should do.

22
00:01:20,220 --> 00:01:24,690
Pause the video, think about the 10 steps and try to apply it here.

23
00:01:25,140 --> 00:01:28,620
Now, just as a quick note, unlike the previous debugging exercises,

24
00:01:28,860 --> 00:01:32,640
there's more than one bug in this code. So, good luck

25
00:01:32,760 --> 00:01:34,020
and I'll see you on the other side.

26
00:01:38,120 --> 00:01:38,630
All right.

27
00:01:38,630 --> 00:01:39,800
So, as I mentioned,

28
00:01:39,860 --> 00:01:43,490
there's quite a few bugs in here and we're going to fix them one by one.

29
00:01:44,090 --> 00:01:44,630
As always,

30
00:01:44,630 --> 00:01:48,410
the first thing we're gonna do is we're just gonna run the code and see what

31
00:01:48,410 --> 00:01:49,243
happens.

32
00:01:49,430 --> 00:01:53,810
So the first thing you'll notice that's a little bit odd is it's printing

33
00:01:53,840 --> 00:01:56,240
instead of just one, two, three, four,

34
00:01:56,600 --> 00:02:00,260
it's printing it as if it's a list with the square bracket around it.

35
00:02:00,710 --> 00:02:04,730
So let's see if we can identify why. Well in our code,

36
00:02:04,940 --> 00:02:09,320
if none of these if statements are true, then it prints the number.

37
00:02:09,680 --> 00:02:11,690
But in this case it's not just the number.

38
00:02:12,110 --> 00:02:14,900
There seems to be some square brackets around the number,

39
00:02:15,230 --> 00:02:20,230
in which case it's printing a list with that number as the single item inside.

40
00:02:21,710 --> 00:02:24,380
So let's get rid of that and let's run our code again.

41
00:02:24,440 --> 00:02:29,330
That looks a little bit better, a bit cleaner. Now let's take a look at the code.

42
00:02:29,780 --> 00:02:34,100
So we know that when the number is only divisible by three then it should print

43
00:02:34,130 --> 00:02:34,963
fizz,

44
00:02:35,240 --> 00:02:40,240
but three, in this case, is actually printing Fizzbuzz, and five is printing Fizzbuzz

45
00:02:41,960 --> 00:02:46,580
and then three and four, it seems a bit of a mumbo-jumbo.

46
00:02:47,300 --> 00:02:49,130
So, what's actually happening here?

47
00:02:50,060 --> 00:02:53,210
It seems like instead of just printing fizz,

48
00:02:53,480 --> 00:02:56,000
it's printing Fizzbuzz and the number.

49
00:02:56,210 --> 00:02:59,180
So there must be a problem in our checking code.

50
00:02:59,830 --> 00:03:04,780
So let's think about what our code needs to do. When the number is divisible by

51
00:03:04,870 --> 00:03:09,400
both three and five, then instead of the number, it should print Fizzbuzz.

52
00:03:09,760 --> 00:03:12,700
So Fizzbuzz should only be printed

53
00:03:13,120 --> 00:03:18,120
if the number that we're currently checking is divisible by both three and five.

54
00:03:19,630 --> 00:03:21,610
Let's use the print statement to help us.

55
00:03:22,240 --> 00:03:27,240
If we use an f-string and we say that we are currently on number and then we

56
00:03:30,670 --> 00:03:33,730
put the number in here. This way

57
00:03:33,730 --> 00:03:36,820
we'll actually print out the current number we're looping through

58
00:03:37,120 --> 00:03:40,810
and then we can see what's being printed each time. So let's run the code.

59
00:03:41,320 --> 00:03:45,910
And if we scroll to the top, currently on number one it prints one,

60
00:03:45,940 --> 00:03:49,270
currently on number two it prints two, currently on number three

61
00:03:49,270 --> 00:03:53,980
it prints Fizzbuzz, Fizz and three. So that is very,

62
00:03:53,980 --> 00:03:55,510
very strange behavior indeed.

63
00:03:55,510 --> 00:04:00,510
It seems like lots of things are true in our if statement. What's happening? Well,

64
00:04:03,610 --> 00:04:05,800
if number is equal to three,

65
00:04:06,460 --> 00:04:09,220
then it seems to be printing Fizzbuzz,

66
00:04:09,580 --> 00:04:14,410
which means if we walk backwards, this if statement must have been true.

67
00:04:14,950 --> 00:04:18,820
Now three divided by three should have zero as the remainder,

68
00:04:18,820 --> 00:04:22,720
so this should be true. But this can't be true,

69
00:04:22,720 --> 00:04:27,720
so this must be false. And it should only print Fizzbuzz if it's divisible by

70
00:04:28,990 --> 00:04:32,590
both three and five. So instead of an or,

71
00:04:32,890 --> 00:04:34,570
this really needs to be an 'and'.

72
00:04:34,900 --> 00:04:39,850
So let's hit run again and see what happens this time. Currently on number three,

73
00:04:39,850 --> 00:04:44,050
it prints Fizz and three. So that's again,

74
00:04:44,080 --> 00:04:47,920
a bit of a problem. So we fixed the Fizzbuzz parts.

75
00:04:48,100 --> 00:04:51,430
It is now printing Fizzbuzz correctly. Namely,

76
00:04:51,460 --> 00:04:56,290
when it's on number 15. But why is it still printing the number?

77
00:04:57,190 --> 00:05:02,190
The number should only be printed if none of the others are true.

78
00:05:03,970 --> 00:05:08,970
But instead, it seems to be printing Fizz and then also printing the number.

79
00:05:10,540 --> 00:05:12,910
What's the reason here? Well,

80
00:05:12,940 --> 00:05:17,800
notice how we've got an else statement here that triggers the printing of the

81
00:05:17,800 --> 00:05:22,570
number. Now, an else is paired with an if. So in this case,

82
00:05:22,570 --> 00:05:27,190
what's happening is it's checking if the number is not divisible by five,

83
00:05:27,520 --> 00:05:31,780
then print the number. That's not what we want. And in fact,

84
00:05:31,900 --> 00:05:33,460
if you look at the print statements,

85
00:05:33,700 --> 00:05:37,870
you can see that only when the number is divisible by five

86
00:05:38,140 --> 00:05:41,590
does it not print the number. When it's divisible by three,

87
00:05:41,860 --> 00:05:45,700
it's still printing the number. This is not what we want.

88
00:05:45,730 --> 00:05:50,650
What we want is for this to be checked, and if that's not true,

89
00:05:50,950 --> 00:05:54,610
then check the next thing. And if that is not true, then the next thing.

90
00:05:54,910 --> 00:05:59,300
And finally, if all three of these are not true, then print the number.

91
00:06:00,080 --> 00:06:05,080
So what we actually need instead of a whole bunch of ifs is actually some else

92
00:06:05,810 --> 00:06:06,560
ifs.

93
00:06:06,560 --> 00:06:11,560
So let's replace those two ifs with some elifs and now let's run our code.

94
00:06:13,570 --> 00:06:17,620
So currently on number one, two, three,

95
00:06:17,680 --> 00:06:22,600
so three now prints Fizz, five prints Buzz, nine prints

96
00:06:22,600 --> 00:06:26,770
Fizz, 10 prints Buzz and 15 prints Fizzbuzz.

97
00:06:27,130 --> 00:06:30,550
So I think we've actually managed to fix all of our code.

98
00:06:30,910 --> 00:06:33,880
So I think we should be able to remove our print statement now.

99
00:06:34,300 --> 00:06:39,300
And indeed we've managed to successfully solve all of the debugging problems

100
00:06:39,550 --> 00:06:43,090
here. Now did you manage to get all that right? Don't worry

101
00:06:43,090 --> 00:06:45,700
if you have to struggle a little bit. In the future,

102
00:06:45,730 --> 00:06:49,540
you're going to be making a lot more bugs and you're going to be repeating this

103
00:06:49,540 --> 00:06:52,060
process of using the 10 steps

104
00:06:52,270 --> 00:06:57,270
to try and identify the problems and fix the code and make it free of bugs.

105
00:06:58,330 --> 00:07:00,970
So hope you had a fun and jam packed day.

106
00:07:01,390 --> 00:07:05,710
Now this might be a good time to revisit some code that you've given up on a

107
00:07:05,710 --> 00:07:08,650
long time ago or projects that you've started

108
00:07:08,680 --> 00:07:12,700
but you've stopped because you ran into bugs. And then review them,

109
00:07:12,760 --> 00:07:17,760
use what you learned today and try to fix the bugs and get your code working.

110
00:07:18,790 --> 00:07:21,790
Now tomorrow, I've got a big day for you coming up.

111
00:07:22,090 --> 00:07:26,500
We're going to spend the whole day creating a big project and we're going to be

112
00:07:26,500 --> 00:07:29,950
making the Higher-Lower game. So I'm looking forward to it,

113
00:07:30,220 --> 00:07:33,730
I hope you are too. So get a good night's rest and I'll see you tomorrow.

