1
00:00:00,510 --> 00:00:02,670
In this coding exercise

2
00:00:02,670 --> 00:00:05,850
you are going to write a program

3
00:00:05,850 --> 00:00:08,940
that is often asked for at coding interviews,

4
00:00:08,940 --> 00:00:11,850
so if you are looking for a job, take note.

5
00:00:11,850 --> 00:00:16,850
Very often the examiner or the interviewer might ask you

6
00:00:17,400 --> 00:00:21,090
write a program to recreate the FizzBuzz game.

7
00:00:21,090 --> 00:00:23,100
Now, if you've never played the FizzBuzz game

8
00:00:23,100 --> 00:00:24,420
it's pretty simple,

9
00:00:24,420 --> 00:00:26,820
let's say you have five friends,

10
00:00:26,820 --> 00:00:28,500
you stand in a circle

11
00:00:28,500 --> 00:00:33,500
and each friend calls out the next number starting from 1.

12
00:00:33,810 --> 00:00:38,810
Now, however, the game starts when you come across a number

13
00:00:39,720 --> 00:00:44,720
that is divisible by 3 then instead you should say Fizz.

14
00:00:45,810 --> 00:00:48,990
And if you come across a number that's divisible by 5

15
00:00:48,990 --> 00:00:51,870
then instead of saying the number, you should say Buzz.

16
00:00:51,870 --> 00:00:53,970
And if the number is divisible

17
00:00:53,970 --> 00:00:57,420
by both 3 and 5, for example, 15

18
00:00:57,420 --> 00:01:01,110
then instead of saying the number, you say, FizzBuzz.

19
00:01:01,110 --> 00:01:05,459
This is a math game that has tormented many of the children

20
00:01:05,459 --> 00:01:10,460
on a long road trip by their parents, including myself.

21
00:01:10,708 --> 00:01:13,740
But in this case, we're going to convert it

22
00:01:13,740 --> 00:01:17,100
into code so that it can do it for us.

23
00:01:17,100 --> 00:01:19,290
Your program is going to print each number

24
00:01:19,290 --> 00:01:23,370
from 1 to 100 in turn, so starting from 1, 2,

25
00:01:23,370 --> 00:01:25,140
but when it comes across the number

26
00:01:25,140 --> 00:01:26,520
that's divisible by 3,

27
00:01:26,520 --> 00:01:31,050
it should print Fizz, divisible by 5 Buzz,

28
00:01:31,050 --> 00:01:35,070
and if divisible by both, it's going to print FizzBuzz.

29
00:01:35,070 --> 00:01:37,410
Take a look inside the Description pane

30
00:01:37,410 --> 00:01:39,990
and see how it might look.

31
00:01:39,990 --> 00:01:44,790
And I only go up to 15, but your goal is to go up to 100

32
00:01:44,790 --> 00:01:47,760
and make sure that you are using code to do this

33
00:01:47,760 --> 00:01:50,040
instead of just working it out.

34
00:01:50,040 --> 00:01:53,400
And remember that your answer should start from 1

35
00:01:53,400 --> 00:01:57,360
and print up to and including 100.

36
00:01:57,360 --> 00:02:00,600
Now, 100 might not be 100 might be Fizz, might be Buzz,

37
00:02:00,600 --> 00:02:01,950
it might be FizzBuzz,

38
00:02:01,950 --> 00:02:04,170
you're going to have to use your code to work it out

39
00:02:04,170 --> 00:02:06,390
and each number of text should be printed

40
00:02:06,390 --> 00:02:07,410
on a separate line.

41
00:02:07,410 --> 00:02:09,240
Those are the rules.

42
00:02:09,240 --> 00:02:13,293
Now have a go and see if you can complete this challenge.

43
00:02:16,560 --> 00:02:20,160
First thing I'm going to do is to set a target

44
00:02:20,160 --> 00:02:22,950
and we're setting our target to 100.

45
00:02:22,950 --> 00:02:26,400
And then I'm going to use that target to create a range.

46
00:02:26,400 --> 00:02:29,640
Now remember, as you saw in the previous coding exercise,

47
00:02:29,640 --> 00:02:33,330
the range() function takes a starting number

48
00:02:33,330 --> 00:02:36,570
and an ending number, but it doesn't include

49
00:02:36,570 --> 00:02:37,530
that ending number.

50
00:02:37,530 --> 00:02:40,770
So we have to add one to our target in order

51
00:02:40,770 --> 00:02:43,530
to create a range up to 100.

52
00:02:43,530 --> 00:02:45,660
Now, once we've got our range,

53
00:02:45,660 --> 00:02:49,350
it's going to increment through the range by 1

54
00:02:49,350 --> 00:02:51,960
because we haven't specified otherwise,

55
00:02:51,960 --> 00:02:54,690
and then we're going to loop through each of the numbers

56
00:02:54,690 --> 00:02:56,913
in our range from 1 to 100.

57
00:02:57,900 --> 00:02:59,130
Now, the first thing we're going to check

58
00:02:59,130 --> 00:03:02,640
is if the number that we're currently looping through

59
00:03:02,640 --> 00:03:07,640
can be cleanly divided by 3 and cleanly divided by 5,

60
00:03:08,670 --> 00:03:11,340
then we're going to print FizzBuzz.

61
00:03:11,340 --> 00:03:12,810
The order matters,

62
00:03:12,810 --> 00:03:17,460
remember, an if statement starts checking from the top

63
00:03:17,460 --> 00:03:22,460
and the elif's come if the first ones are false.

64
00:03:22,830 --> 00:03:25,260
So if we first checked for,

65
00:03:25,260 --> 00:03:28,200
if the number is just cleanly divisible by 3

66
00:03:28,200 --> 00:03:29,580
and we print Fizz

67
00:03:29,580 --> 00:03:31,650
then we're going to miss the situation

68
00:03:31,650 --> 00:03:34,710
where it's also divisible by 5.

69
00:03:34,710 --> 00:03:37,350
This was the important thing to realize

70
00:03:37,350 --> 00:03:39,840
in the logic of this program

71
00:03:39,840 --> 00:03:43,830
and maybe you worked it out by trying some things out

72
00:03:43,830 --> 00:03:46,290
or by realizing it in the beginning

73
00:03:46,290 --> 00:03:49,260
but this is something that you might have missed

74
00:03:49,260 --> 00:03:51,180
and you might need to go back and check.

75
00:03:51,180 --> 00:03:53,850
Now, the subsequent parts are pretty simple.

76
00:03:53,850 --> 00:03:56,190
If the first condition is not met,

77
00:03:56,190 --> 00:03:59,160
so it's not a number that can be divided by 3

78
00:03:59,160 --> 00:04:02,130
and 5 at the same time, then we're going to  check,

79
00:04:02,130 --> 00:04:04,530
well, is it divisible by three cleanly?

80
00:04:04,530 --> 00:04:06,510
In which case we'll print Fizz.

81
00:04:06,510 --> 00:04:09,000
Otherwise, is it divisible by 5 cleanly,

82
00:04:09,000 --> 00:04:10,590
in which we'll print Buzz

83
00:04:10,590 --> 00:04:13,530
and otherwise, if it doesn't divide by any of those numbers

84
00:04:13,530 --> 00:04:15,510
and doesn't meet any of those conditions,

85
00:04:15,510 --> 00:04:17,850
we just print the number.

86
00:04:17,850 --> 00:04:20,430
And because we're looping through the numbers one by one

87
00:04:20,430 --> 00:04:23,820
we're going to get these printed out in sequence one by one

88
00:04:23,820 --> 00:04:26,823
and checking for each of these criterias.

