1
00:00:05,280 --> 00:00:08,039
So how did you get on?
There are lots of ways you may have

2
00:00:08,039 --> 00:00:12,320
written your code. The real test
is that it works. Alright, so let's go

3
00:00:12,320 --> 00:00:15,360
through our solution.
I'm going to start with some very brief

4
00:00:15,360 --> 00:00:20,320
instructions - very brief instructions -
and get the player to press enter to start.

5
00:00:20,320 --> 00:00:24,480
So let's have a go at doing that.
I'm going to start down here. I'm going

6
00:00:24,480 --> 00:00:29,519
to remove this for loop -
start on line 21. I'm going to type input

7
00:00:29,519 --> 00:00:35,920
parentheses double quotes Play
Fizz Buzz. Full stop,

8
00:00:35,920 --> 00:00:40,640
a couple of spaces there, Press ENTER to start

9
00:00:40,640 --> 00:00:44,559
double quote right parentheses. Then
we'll do a print

10
00:00:44,559 --> 00:00:48,960
parentheses for a blank line.
Alright, so next we're going to use a

11
00:00:48,960 --> 00:00:52,640
while loop, and increase
a counter each time the computer and the

12
00:00:52,640 --> 00:00:57,920
player says their number.
On line 24, next_number

13
00:00:57,920 --> 00:01:02,620
is equal to zero.
Let me move this up a little bit actually.

14
00:01:02,620 --> 00:01:04,080
And we're going to do that while loop

15
00:01:04,080 --> 00:01:10,159
that I talked about, so while
next_number is less than 99

16
00:01:10,159 --> 00:01:17,600
colon, next_number plus equals one.
Then on the next line, print

17
00:01:17,600 --> 00:01:23,439
parentheses fizz_buzz,
and in parentheses, we're passing next

18
00:01:23,440 --> 00:01:29,820
number, so next_number.
So we've initialized next_number to zero,

19
00:01:29,820 --> 00:01:33,840
then increase it by one
before each of the computers goes.

20
00:01:33,840 --> 00:01:38,560
The computer generates this number
and prints it on line 27.

21
00:01:38,560 --> 00:01:40,980
Alright, so now it's the player's turn.

22
00:01:40,980 --> 00:01:43,440
So we  need to increment
next_number again,

23
00:01:43,440 --> 00:01:47,980
and use it to calculate the correct answer.
Then we get the player's input.

24
00:01:47,980 --> 00:01:53,020
Alright, so next_number plus equals one.

25
00:01:53,020 --> 00:01:56,159
Alright, then correct answer

26
00:01:56,160 --> 00:02:03,540
is equal to fizz_buzz,
and we're passing next_number to that

27
00:02:03,540 --> 00:02:07,600
players_answer, on the next line,

28
00:02:07,600 --> 00:02:11,440
is equal to input parenthesis double
quotes,

29
00:02:11,440 --> 00:02:14,879
Your go colon and a space, then we've
got the double quote

30
00:02:14,879 --> 00:02:19,599
and the right parenthesis. Because we
have to increment next number twice,

31
00:02:19,599 --> 00:02:24,080
a while loop is more suitable than a for
loop. It is possible to use a for loop,

32
00:02:24,080 --> 00:02:27,200
with a step of two, but it
does get a bit messy.

33
00:02:27,200 --> 00:02:30,400
If you want to try a for loop, to see the
problems you get, then wait until I've

34
00:02:30,400 --> 00:02:33,120
shown you how to cheat,
at the end of this video.

35
00:02:33,120 --> 00:02:37,360
To check if the player provided the
correct number - or fizz, buzz or fizz buzz -

36
00:02:37,360 --> 00:02:41,360
we now need to compare the input to the
value returned by our function.

37
00:02:41,360 --> 00:02:45,760
So let's do that. So we're going to type if

38
00:02:45,760 --> 00:02:52,319
players_answer is not equal
to correct_answer colon.

39
00:02:52,320 --> 00:02:55,380
We need to print a message here to say
they got it wrong. So print

40
00:02:55,380 --> 00:02:59,520
and in parentheses, double quotes
You lose,

41
00:03:00,080 --> 00:03:06,800
the correct answer
was, left and right curly braces double

42
00:03:06,800 --> 00:03:10,480
quote dot format parentheses,
correct_answer

43
00:03:10,480 --> 00:03:13,280
in those parentheses, and then
we're closing that off.

44
00:03:13,280 --> 00:03:16,959
Then on the next line,
we need to do a break.

45
00:03:16,959 --> 00:03:21,120
They get it wrong, we need to break out
of the loop. So looking at lines 26

46
00:03:21,120 --> 00:03:26,799
through 28, we increment next number
before calling the fizz_buzz function.

47
00:03:26,799 --> 00:03:30,959
Inside the loop, next_number gets
increased by 1 twice -

48
00:03:30,959 --> 00:03:34,159
once before the computer's response and
once before checking

49
00:03:34,159 --> 00:03:37,280
what the player enters.
Okay, so that should work.

50
00:03:37,280 --> 00:03:40,239
It would be nice to let the player know
that they're finished, though. If we get

51
00:03:40,239 --> 00:03:43,120
to the end, the while loop
will terminate normally -

52
00:03:43,120 --> 00:03:46,239
we don't break out of it.
Now if you remember from the Program

53
00:03:46,239 --> 00:03:50,799
flow control in Python section,
an else block can be used to execute

54
00:03:50,799 --> 00:03:53,439
code, only when the loop
terminates normally.

55
00:03:53,439 --> 00:03:58,239
So let's add an else here on
line 34, an else to the while statement

56
00:03:58,240 --> 00:04:05,140
on line 25; else colon print 
parentheses double quotes Well done,

57
00:04:05,140 --> 00:04:08,880
you reached, then left
and right curly braces,

58
00:04:08,880 --> 00:04:14,000
double quote dot format
and next_number in parentheses,

59
00:04:14,000 --> 00:04:17,260
and closing off that line.

60
00:04:17,260 --> 00:04:20,798
Alright, so that should be that.
We're now ready to play fizz buzz

61
00:04:20,798 --> 00:04:23,120
and it's harder than it sounds.
Alright, so let's run

62
00:04:23,120 --> 00:04:25,759
the program now. I'll have a
couple of goes myself.

63
00:04:25,759 --> 00:04:28,880
Alright, so I'm going to start
by pressing ENTER to start,

64
00:04:28,880 --> 00:04:33,680
1 Your go. The next number is 2 -
that's not fizz, buzz or fizz buzz,

65
00:04:33,680 --> 00:04:37,040
so we'll put 2 there. 3, which is correct,

66
00:04:37,040 --> 00:04:42,540
because the number three is divisible
by three, so I'll put four in there, buzz.

67
00:04:42,540 --> 00:04:45,919
We're up to five - five was chosen by
the computer -

68
00:04:45,919 --> 00:04:50,800
so I'm gonna put six there, and of course,
I screwed that up because

69
00:04:50,800 --> 00:04:54,639
six is divisible by three. Again, it's
harder than it sounds if you're not

70
00:04:54,639 --> 00:04:57,199
really paying attention.
Alright, so have a go yourself and see

71
00:04:57,199 --> 00:05:00,080
how far you can get on your first try. I'm
a bit embarrassed to say that I only got

72
00:05:00,080 --> 00:05:02,880
up to number six.
Getting to 100 actually isn't easy,

73
00:05:02,880 --> 00:05:06,479
unless you're really concentrating.
Alright, so to test that we get the

74
00:05:06,479 --> 00:05:11,680
Well done message printed out on
line 35 - close down the run window now -

75
00:05:11,680 --> 00:05:15,120
we're going to actually cheat a bit, because obviously,
to get to 100 will take some time.

76
00:05:15,120 --> 00:05:20,400
So i'm going to comment out the input, and
bind the player's answer to the correct one.

77
00:05:20,400 --> 00:05:25,280
So I'm gonna come up here to line 30.
I'm going to comment that out,

78
00:05:25,280 --> 00:05:28,800
and instead what I'm going to do,
is add a line under that

79
00:05:28,800 --> 00:05:36,860
and put players_answer is
equal to correct_answer.

80
00:05:36,860 --> 00:05:39,840
So by doing that, the player's
answer will always be correct,

81
00:05:39,840 --> 00:05:43,060
and we can test what happens 
when the player gets to the end.

82
00:05:43,060 --> 00:05:46,240
So if we actually run this now.

83
00:05:46,240 --> 00:05:49,680
Now I'm not suggesting that you cheat
when testing your code. I've done this to

84
00:05:49,680 --> 00:05:53,120
be reasonably sure that there isn't a
serious bug in the code.

85
00:05:53,120 --> 00:05:58,479
Close down that run window. Now the next
step is to swap the commenting around,

86
00:05:58,479 --> 00:06:03,680
on lines 30 and 31 which I'm doing now,
and to play the game properly.

87
00:06:03,680 --> 00:06:07,600
This will take a while, which is why I
cheated. We've shown that there's nothing

88
00:06:07,600 --> 00:06:11,980
seriously wrong with this code. We can 
now spend the time to test it properly.

89
00:06:11,980 --> 00:06:14,080
Don't take shortcuts when testing.

90
00:06:14,080 --> 00:06:17,919
A quick cheat, to verify that it's
possible to get to the end, is valid,

91
00:06:17,919 --> 00:06:21,039
but it's not a substitute
for thorough testing.

92
00:06:21,039 --> 00:06:24,560
Alright, so have fun playing the game, and
then have a go at the exercise coming up.

93
00:06:24,560 --> 00:06:28,800
I'll see you in the next video.

