1
00:00:00,180 --> 00:00:03,300
All right guys, I've got a fun coding challenge for you today,

2
00:00:03,570 --> 00:00:06,450
and it is a problem called fizzbuzz.

3
00:00:06,960 --> 00:00:11,910
Now this is probably one of the most asked questions at interviews for

4
00:00:11,910 --> 00:00:14,910
programmers because it tests the way that you think,

5
00:00:15,150 --> 00:00:20,150
and it allows the interviewer to see how you use your logic to solve a problem.

6
00:00:21,840 --> 00:00:24,960
And the problem we're going to tackle is a children's game called

7
00:00:24,960 --> 00:00:29,850
Fizzbuzz. Last time I checked the programmatic solution was actually up on

8
00:00:29,850 --> 00:00:30,540
Wikipedia.

9
00:00:30,540 --> 00:00:34,920
So I recommend reading the Wikipedia article only after you've solved this

10
00:00:34,920 --> 00:00:38,280
Programming challenge. The rules of fizzbuzz are really simple.

11
00:00:38,910 --> 00:00:42,990
Basically, you have a bunch of kids who sit around in a circle and going

12
00:00:42,990 --> 00:00:46,680
clockwise. The first kid says 1, the second kid says 2.

13
00:00:47,100 --> 00:00:51,810
But then when a kid encounters a number that's fully divisible by three,

14
00:00:52,320 --> 00:00:55,920
then instead of saying that number, he's going to say Fizz.

15
00:00:57,180 --> 00:00:59,310
And when the number is divisible by five,

16
00:00:59,610 --> 00:01:02,220
instead of saying the number, they should say Buzz.

17
00:01:02,820 --> 00:01:06,900
And if the number is divisible by both three and five, for example, 15,

18
00:01:07,200 --> 00:01:08,940
then they should say Fizzbuzz.

19
00:01:09,420 --> 00:01:14,420
So your program is going to replicate this for all the numbers up to and

20
00:01:14,610 --> 00:01:18,840
including a hundred. To start off, it will probably look something like this.

21
00:01:18,900 --> 00:01:20,970
It's going to print one, and then on the next line

22
00:01:20,970 --> 00:01:23,130
it will print two and then fizz,

23
00:01:23,160 --> 00:01:27,030
because three is divisible by three. And then four,

24
00:01:27,210 --> 00:01:29,760
and then five is divisible by five so it's a buzz.

25
00:01:30,390 --> 00:01:34,980
And then six is fizz because it's divisible by three and so on and so forth.

26
00:01:35,460 --> 00:01:39,030
This is what your program has to do. Take a look at the instructions,

27
00:01:39,330 --> 00:01:42,150
make sure that you understand what it is that your program has to do

28
00:01:42,510 --> 00:01:44,880
and then pause the video and try to give this a go.

29
00:01:48,920 --> 00:01:52,760
All right. So the first thing we have to do is to create a range, right?

30
00:01:52,820 --> 00:01:57,820
So let's say that for number in range, and our range is between 1 and 100,

31
00:01:59,930 --> 00:02:02,120
so we have to include 101 in here.

32
00:02:02,840 --> 00:02:07,460
And what we want to do is we want to check whether if the number's divisible by

33
00:02:07,460 --> 00:02:09,140
three, divisible by five,

34
00:02:09,410 --> 00:02:12,920
or if it's divisible by both three and five.

35
00:02:13,760 --> 00:02:18,200
Now we could write our code by doing this in order, right? We could say

36
00:02:18,260 --> 00:02:22,370
if number % 3 is equal to zero,

37
00:02:22,580 --> 00:02:24,320
then it's divisible

38
00:02:25,700 --> 00:02:26,750
by three.

39
00:02:28,700 --> 00:02:32,090
So you might think that you could write the code like this,

40
00:02:32,390 --> 00:02:35,720
but there is a crucial problem in the logic here

41
00:02:36,140 --> 00:02:40,790
namely that we know that the if, elif, else statements will stop

42
00:02:41,090 --> 00:02:44,750
once it's found one statement that's true. For example,

43
00:02:44,990 --> 00:02:47,420
if we take the number to be 15,

44
00:02:47,870 --> 00:02:50,420
15 divided by three is equal to zero

45
00:02:50,780 --> 00:02:55,130
so it's going to carry out whatever instruction is inside this if statement

46
00:02:55,430 --> 00:02:58,490
and then it's going to be done with it. It's going to skip this one

47
00:02:58,670 --> 00:03:01,180
and crucially, it's going to skip this one

48
00:03:01,510 --> 00:03:06,510
which is what we really need. Instead of structuring our code like this,

49
00:03:06,880 --> 00:03:10,030
what you might have done and what you should've done is in fact,

50
00:03:10,030 --> 00:03:12,190
have this at the top.

51
00:03:12,550 --> 00:03:17,080
So first check if it's divisible by three and by five,

52
00:03:17,740 --> 00:03:19,240
and then if that is not true,

53
00:03:19,570 --> 00:03:24,100
then check to see if it's divisible by five and then divisible by three or the

54
00:03:24,100 --> 00:03:25,300
other option works as well.

55
00:03:25,300 --> 00:03:30,250
You could do it like this. Because these two have no overlap in terms of the

56
00:03:30,250 --> 00:03:32,320
results and then it doesn't really matter.

57
00:03:32,890 --> 00:03:37,180
So now we can finally write our code because we can say, well,

58
00:03:37,180 --> 00:03:41,530
if the number that we're currently encountering is divisible by both three and

59
00:03:41,530 --> 00:03:44,140
five, then we have to print Fizzbuzz.

60
00:03:46,900 --> 00:03:50,710
If it's only divisible by three, then we're going to print fizz.

61
00:03:52,510 --> 00:03:55,780
And if it's only divisible by five, then we're going to print buzz.

62
00:03:58,470 --> 00:03:59,160
All right.

63
00:03:59,160 --> 00:04:02,400
Now that still leaves the remaining numbers, right?

64
00:04:02,430 --> 00:04:03,810
Because the remaining numbers

65
00:04:03,840 --> 00:04:07,950
which don't fit any of the criteria should just be printed out as the number.

66
00:04:08,460 --> 00:04:13,140
We can catch that quite easily with an else statement that catches basically every

67
00:04:13,140 --> 00:04:16,770
other number, in which case, we're just going to print the number.

68
00:04:17,490 --> 00:04:20,310
So now, if I go ahead and run this code,

69
00:04:20,820 --> 00:04:25,260
then you can see that all the way from 1 to 100,

70
00:04:25,620 --> 00:04:29,220
it's been able to work out which ones are fizz, which ones are buzz,

71
00:04:29,280 --> 00:04:33,390
and which ones are fizzbuzz. How did you get on with it?

72
00:04:33,750 --> 00:04:38,340
If you got stuck or if you don't understand why this logic matters,

73
00:04:38,640 --> 00:04:42,720
then I want you to try both versions that I showed you and see it

74
00:04:42,720 --> 00:04:46,650
step-by-step using the Thonny program just as we did before.

75
00:04:47,790 --> 00:04:51,780
That way you can visualize what's actually happening in terms of the logic and

76
00:04:51,780 --> 00:04:55,950
how it's flowing from one step to the next, to the next. Now,

77
00:04:55,980 --> 00:04:59,970
once you've done that, then go back to your exercise and try to complete it.

78
00:05:00,240 --> 00:05:03,990
It's really, really important that you don't just say, "Oh yeah, I understand

79
00:05:03,990 --> 00:05:07,590
now." It's a really good idea to make sure that your code actually works at the

80
00:05:07,590 --> 00:05:11,760
end of the day and to write the code yourself. Today

81
00:05:11,760 --> 00:05:16,760
we've learned all about loops and we're going to apply it in our final project.

82
00:05:17,640 --> 00:05:20,520
So that's what's waiting up for you in the next lesson.

83
00:05:20,820 --> 00:05:22,230
So head over that once you're ready.

