1
00:00:05,359 --> 00:00:08,800
We've digressed a bit in the last few
videos but there was some useful stuff

2
00:00:08,800 --> 00:00:09,280
in there.

3
00:00:54,140 --> 00:00:55,440
So let's do that.

4
00:00:55,440 --> 00:00:58,000
We're going to start out by
creating a new Python file.

5
00:00:58,000 --> 00:01:02,480
Let's call this one
hilo_test.py.

6
00:01:07,040 --> 00:01:09,580
Now you don't want to type in all the
code from the hilo game again,

7
00:01:09,580 --> 00:01:12,479
so I'm going to paste it in.
You'll find the code in the resources

8
00:01:12,480 --> 00:01:17,220
for this video,
in hilo_test.txt.

9
00:01:17,220 --> 00:01:20,799
Now you'll get errors, just as I have,
when you paste it in. That's fine.

10
00:01:20,799 --> 00:01:23,759
We'll be fixing them in a moment.
First, though, I want to go through the

11
00:01:23,759 --> 00:01:26,240
changes we've had to make,
when putting the game

12
00:01:26,240 --> 00:01:30,320
into a function. We have to make a few
changes to the code, if we're going to

13
00:01:30,320 --> 00:01:35,280
automate the testing. The obvious change
is, that we no longer want user input.

14
00:01:35,280 --> 00:01:39,200
So looking at the code - if we scroll
back up to lines four and five.

15
00:01:39,200 --> 00:01:42,560
I've commented out the input code on
those lines, and also on lines

16
00:01:42,560 --> 00:01:49,400
13 and 14. After doing that, we no
longer have a high_low variable to test.

17
00:01:49,400 --> 00:01:52,320
Instead, we test the guess against the answer,

18
00:01:52,320 --> 00:01:59,920
and that's the conditions on line 17, lines 21 and 25.

19
00:01:59,920 --> 00:02:01,680
And you can see the original conditions

20
00:02:01,680 --> 00:02:05,280
commented out on the lines above.
Now the else clause,

21
00:02:05,280 --> 00:02:09,220
as you scroll down a little bit further,
on line 29 and 30,

22
00:02:09,220 --> 00:02:13,200
is also commented out. Normally,
you delete all those lines, rather than

23
00:02:13,200 --> 00:02:16,319
commenting them out.
I've left them in, so you can see what

24
00:02:16,319 --> 00:02:19,440
we've changed.
Instead of printing a message,

25
00:02:19,440 --> 00:02:24,180
our function will return the guess -
that's the changes on lines 26 through 28.

26
00:02:24,180 --> 00:02:28,000
We return the number of guesses,
instead of breaking out of the loop.

27
00:02:28,000 --> 00:02:32,000
Other than those changes, the main loop
is the same as it was originally.

28
00:02:32,000 --> 00:02:36,000
Now put all the code into a function
called guess_binary,

29
00:02:36,000 --> 00:02:39,040
starting up here on line 8.
So it takes

30
00:02:39,040 --> 00:02:43,360
three arguments, as you can see.
We tell it what the answer is,

31
00:02:43,360 --> 00:02:46,800
otherwise it won't know when it's got
the correct answer. Providing the answer

32
00:02:46,800 --> 00:02:53,680
allows the new conditions on line 17,
lines 21 and lines 25, to work.

33
00:02:53,680 --> 00:02:58,380
In addition, we also give it the low and
high values for the range we're guessing in.

34
00:02:58,380 --> 00:03:00,000
The code in our function is now using

35
00:03:00,000 --> 00:03:03,840
the low and high parameters,
rather than the global parameters,

36
00:03:03,840 --> 00:03:06,959
on lines 1 and 2.
We're actually getting a warning for low

37
00:03:06,959 --> 00:03:10,400
and high on line eight, that our
parameter names shadow the names

38
00:03:10,400 --> 00:03:13,200
from the outer scope.

39
00:03:15,840 --> 00:03:19,040
Avoid using the same names
for your parameters,

40
00:03:19,040 --> 00:03:23,760
as the variables in your main code. It can
get very confusing, when you read the code,

41
00:03:23,760 --> 00:03:25,920
to work out what's
being referred to.

42
00:03:25,920 --> 00:03:30,080
That's what this warning actually means -
the parameters have the same name as

43
00:03:30,080 --> 00:03:34,560
variables in the main code.
So what I'm going to do is rename the

44
00:03:34,560 --> 00:03:38,520
variables on lines 1 and 2.
They're constant values, so I'll

45
00:03:38,520 --> 00:03:43,680
change them to uppercase.
So LOW, LOW in uppercase,

46
00:03:43,680 --> 00:03:49,840
and same for high - HIGH will now be also
in uppercase, and that's removed the

47
00:03:49,840 --> 00:03:54,879
warnings on line 8, as you can see there.
And also scrolling down, it's also fixed

48
00:03:54,879 --> 00:03:59,680
the errors in the loop,
from lines 35 onwards.

49
00:03:59,680 --> 00:04:03,120
I'd already used those new names
in this block of code.

50
00:04:03,120 --> 00:04:08,560
The main code, on lines 35 to 37,
is a loop. It calls our guest binary

51
00:04:08,560 --> 00:04:11,280
function, passing every
number in the range.

52
00:04:11,280 --> 00:04:15,500
We then print out the number being
guessed, and the number of guesses it took.

53
00:04:15,500 --> 00:04:17,120
The reason we're doing this,
is to make

54
00:04:17,120 --> 00:04:20,399
sure that our code will work, for
all the numbers that the player can

55
00:04:20,399 --> 00:04:23,759
choose. Having to choose each number from
one to one thousand,

56
00:04:23,759 --> 00:04:28,479
and keep answering higher or lower for
each one, would literally take hours.

57
00:04:28,479 --> 00:04:32,720
So we're getting the computer to do that
for us. Alright, so let's actually

58
00:04:32,720 --> 00:04:36,080
run the program
to see if it works.

59
00:04:38,400 --> 00:04:41,840
Now we've got a stack of output here,
so what I'm going to do is move the

60
00:04:41,840 --> 00:04:44,960
run pane over to right top.

61
00:04:51,040 --> 00:04:57,040
Alright, so scrolling through, and what I'll
do is, I'll go back up to the start,

62
00:04:57,040 --> 00:05:00,240
and we can see each number that was
guessed and the number of guesses

63
00:05:00,240 --> 00:05:03,759
needed. So that's good,
as I'm scrolling through here.

64
00:05:03,759 --> 00:05:07,120
Our code can guess every number that the
player might choose.

65
00:05:07,120 --> 00:05:12,600
We weren't sure of that until we ran this
test. Obviously, I can go right down now,

66
00:05:12,600 --> 00:05:17,520
literally to a thousand. Now if we make a
mistake in the code, testing it inside

67
00:05:17,520 --> 00:05:20,960
this function should highlight
that there's an error.

68
00:05:20,960 --> 00:05:24,639
So as an example here,
let's make a change. I'm going to modify,

69
00:05:24,640 --> 00:05:29,900
up here line 19, and what 
I'm going to do is delete the + 1,

70
00:05:29,900 --> 00:05:33,440
after the variable name guess.

71
00:05:33,440 --> 00:05:36,400
And if we run it again,

72
00:05:37,199 --> 00:05:41,039
you can see what happened there. It takes
nine guesses to guess the first number,

73
00:05:41,039 --> 00:05:44,240
and then nothing.
Our code isn't finishing.

74
00:05:44,240 --> 00:05:47,600
At this point, it's stuck in an infinite
loop. I'm gonna have to use the stop

75
00:05:47,600 --> 00:05:51,440
button over here, to stop it.
If we'd made that mistake in

76
00:05:51,440 --> 00:05:54,320
our game, our players
wouldn't be very impressed.

77
00:05:54,320 --> 00:05:56,800
We might have missed the bug when we
tested the program,

78
00:05:56,800 --> 00:06:01,199
because it still works for some numbers.
By getting the computer to test every

79
00:06:01,199 --> 00:06:04,400
single number, we can make
sure that our code works.

80
00:06:04,400 --> 00:06:06,720
Note that what we're doing here is testing -

81
00:06:06,720 --> 00:06:09,840
this isn't debugging.

82
00:07:33,759 --> 00:07:36,319
Check out this page which talks

83
00:07:36,319 --> 00:07:41,120
about the heartbleed bug,
if you'd like to know more about it.

84
00:07:41,280 --> 00:07:46,240
there's a link, as always, in the
resources section of this video.

85
00:07:46,240 --> 00:07:49,759
Now there's evidence that the
heartbleed bug was exploited.

86
00:07:49,759 --> 00:07:53,759
You can find a summary of
some of these exploits here,

87
00:07:53,759 --> 00:07:59,520
I'll paste in the link
to the wikipedia page.

88
00:08:26,060 --> 00:08:28,640
Alright, so let's say we had two bugs in our code.

89
00:08:28,640 --> 00:08:35,720
What I'm going to do now is delete
the - 1 from the end of line 23,

90
00:08:35,720 --> 00:08:41,200
like so. When I run the program again,
which I'll do now,

91
00:08:41,200 --> 00:08:44,080
you can see what happened. The program
runs successfully and we got a correct

92
00:08:44,080 --> 00:08:49,440
guess for the first 999 numbers,
but when our code tries to guess 1000,

93
00:08:49,440 --> 00:08:53,519
it goes into an infinite loop again.
Would you have spotted that, when testing

94
00:08:53,519 --> 00:08:56,720
the code manually. Well yes, of course you
would, because you've learnt the

95
00:08:56,720 --> 00:09:01,760
importance of testing edge cases.
Guessing 1 and 1000 are the edge cases

96
00:09:01,760 --> 00:09:05,960
in this code, but a bug that
happens only one time in 1000,

97
00:09:05,960 --> 00:09:08,320
well that could easily be missed.

98
00:09:08,320 --> 00:09:11,120
I'm going to stop the program
and just undo those last two

99
00:09:11,120 --> 00:09:14,640
changes that I made to the source code.

100
00:09:15,920 --> 00:09:19,120
Let's just run it again to make sure it
still works, and it's back to working

101
00:09:19,120 --> 00:09:22,000
okay again.
So putting your code into functions

102
00:09:22,000 --> 00:09:25,279
makes testing easier.
You can test that your functions do what

103
00:09:25,279 --> 00:09:28,640
you expect, then use them in your main code.

104
00:09:28,640 --> 00:09:32,399
Debugging is also easier, because you
only have to concentrate on small

105
00:09:32,399 --> 00:09:36,399
amounts of code.
Debugging a 10000 line program would be

106
00:09:36,399 --> 00:09:38,880
a nightmare, if your code
wasn't split up into

107
00:09:38,880 --> 00:09:42,399
smaller chunks.
Okay, so that's another reason for using

108
00:09:42,400 --> 00:09:45,980
functions - small self-contained bits of code,

109
00:09:45,980 --> 00:09:50,240
that're easier to test and debug.
I'll stop this video here. In the next one,

110
00:09:50,240 --> 00:09:54,160
we'll count how many times
the computer could get the correct value,

111
00:09:54,160 --> 00:10:01,120
without having to be told
it was correct. See you in the next video.

