1
00:00:04,700 --> 00:00:07,760
Alright so we had a warning at the end
of the last video and I just wanted to

2
00:00:07,760 --> 00:00:10,760
check whether you could pick that up. You can
see there, it really gave it away when

3
00:00:10,769 --> 00:00:13,980
I hovered over it: Name guesses can be not
defined, and of course if you look at the

4
00:00:13,980 --> 00:00:19,410
code, guess equals 1 should have been
guesses equals 1. And of course when I

5
00:00:19,410 --> 00:00:24,570
do that, it disappears. Alright so
there's that. Alright, so we've written

6
00:00:24,570 --> 00:00:28,080
the code to get the computer to guess
a number we think of. Let's now see if

7
00:00:28,080 --> 00:00:34,620
it works. So we're gonna run the code, and
we're thinking of a number between 1 and

8
00:00:34,620 --> 00:00:40,130
1000. The number I'm gonna be thinking of
is 837, so I'm gonna press ENTER to start.

9
00:00:40,130 --> 00:00:46,110
The computers guess is 500. Now should it guess
higher or lower, enter h or l or c if my

10
00:00:46,110 --> 00:00:50,780
guess was correct. Now what I'm gonna do
there firstly, just add a space there,

11
00:00:50,780 --> 00:00:55,660
then what we're gonna do is just re-run
this. I'm gonna stop it and run it again, because

12
00:00:55,660 --> 00:00:59,219
that's going to annoy me very quickly.
Press ENTER there. Alright so that's

13
00:00:59,220 --> 00:01:04,660
better. 837 was the number we're guessing.
We can see here, the computer's guessed 500.

14
00:01:04,660 --> 00:01:07,460
Should I guess, the computer's asking,
should I guess higher or lower.

15
00:01:07,460 --> 00:01:11,840
Well obviously, it needs to be higher so I'm
going to enter h there. This time the

16
00:01:11,850 --> 00:01:17,310
computer's guessed 750. It's still too low
because their number's 857, h again in

17
00:01:17,310 --> 00:01:25,080
that case. Computers guess is 875. It
needs to guess lower so we need an l there.

18
00:01:25,080 --> 00:01:30,380
Computers guess is 812 now, or 812
is obviously too low because their

19
00:01:30,380 --> 00:01:38,360
number's 837. So I need to tell it to guess
higher. 843, getting closer now to 837,

20
00:01:38,360 --> 00:01:43,939
so it's actually too high. So we need to go
lower. Next guess, you can see there is

21
00:01:43,939 --> 00:01:50,820
827, which needs to be higher, so I'm
gonna press h there for higher. 835 now -

22
00:01:50,820 --> 00:01:57,780
it's getting close but still a bit too low.
We tell it to go higher again. My guess is 839.

23
00:01:57,780 --> 00:02:00,460
l for low because it's still too high -

24
00:02:00,460 --> 00:02:04,000
our number's 837.
This time you can see he's guessed it,

25
00:02:04,000 --> 00:02:08,060
837. So I'm gonna type c there,
then it's correct,

26
00:02:08,060 --> 00:02:10,679
and after all that time I've been
wondering, well why have we got to guess

27
00:02:10,679 --> 00:02:16,230
in 837 guesses. What have we done wrong
there? Well the simple thing is that

28
00:02:16,230 --> 00:02:18,910
we've actually printed out
the wrong message. If we go down and

29
00:02:18,910 --> 00:02:24,740
have a look, we're printing out guess
which is the last guess, and not guesses.

30
00:02:24,740 --> 00:02:27,760
So I'm going to fix that, and we'll run
it again and this time I'm going to run it

31
00:02:27,760 --> 00:02:32,880
in fast motion, just so we go through it
quickly to confirm that it works then.

32
00:02:34,840 --> 00:02:39,400
Okay, so you can see the computer's now
guessed 837 again, c if we're correct.

33
00:02:39,400 --> 00:02:42,840
This time we've got it in nine guesses, so we've got the
correct output now that I've fixed those

34
00:02:42,840 --> 00:02:47,360
two bugs. Now it can take less than that,
depending on the number you thought of,

35
00:02:47,360 --> 00:02:51,660
but it won't ever need any more than ten
guesses. So try with a few more numbers to

36
00:02:51,670 --> 00:02:57,310
make sure the program seems to work okay.
So this is actually quite a hard program

37
00:02:57,310 --> 00:03:01,750
to test. A simple typing error could
result in the program never guessing

38
00:03:01,750 --> 00:03:05,380
correctly, and we may not detect that
unless we chose a number that causes a

39
00:03:05,380 --> 00:03:08,590
problem. Now before I would be
completely happy with this code,

40
00:03:08,590 --> 00:03:12,670
I'd probably test it for all possible numbers
- so I'd think of each number from 1

41
00:03:12,670 --> 00:03:17,110
to 1,000 in turn. So the next 35 hours of
video would be me pressing h and l,

42
00:03:17,110 --> 00:03:20,910
over and over again. Of course not - that's
not a very good use of our time,

43
00:03:20,910 --> 00:03:26,590
especially when we have computers to
perform repetitive tasks for us.

44
00:03:26,590 --> 00:03:30,880
So later in the course, we'll get the
computer to test this code for us. But we

45
00:03:30,880 --> 00:03:35,560
need to understand about functions first,
so we'll leave it till later. Now one thing

46
00:03:35,560 --> 00:03:39,819
you might want to do, is run the program
in the debugger. I won't do that, but go

47
00:03:39,819 --> 00:03:43,090
ahead and do that if you want - it's a
great way to learn. The reason I'm not

48
00:03:43,090 --> 00:03:45,940
going to do it here is because there's
only two values that we're interested

49
00:03:45,940 --> 00:03:49,959
in, when stepping through the code. The
loop already pauses each time around,

50
00:03:49,959 --> 00:03:53,980
because it waits for our input. Printing
out the values of low and high is much

51
00:03:53,980 --> 00:03:58,840
easier than using the debugger, in this
case. So I'm going to do that at the

52
00:03:58,840 --> 00:04:02,709
start of the loop. So just, I'm going to
close the run pane and go back to

53
00:04:02,709 --> 00:04:06,940
the start of the loop, under the while
statement there. Line nine, what  I'm going to

54
00:04:06,940 --> 00:04:12,920
do is print parenthesis double quotes
backslash t, which is a tab character,

55
00:04:12,920 --> 00:04:19,149
Guessing in the range of, left and right
curly braces, to, and left and right curly

56
00:04:19,149 --> 00:04:24,260
braces again, double quotes dot format,
and in parentheses low comma high,

57
00:04:24,260 --> 00:04:27,960
then we've got our two right parentheses
there. So I've included a tab character,

58
00:04:27,970 --> 00:04:31,750
that's that backslash t at the start of
the output. Indenting the output makes

59
00:04:31,750 --> 00:04:35,770
it easier to pick out the computers guesses,
from the diagnostic information that

60
00:04:35,770 --> 00:04:40,330
we're printing. So let's run the program
again now, and this time we're going to

61
00:04:40,330 --> 00:04:45,880
be using 987 as the number to guess. So I'm gonna
press ENTER to start there.

62
00:04:45,880 --> 00:04:49,420
Alright, so you can see the range is
there, guessing in the range of 1 to 1000.

63
00:04:49,420 --> 00:04:55,140
1987 is higher, so I'm going to press h
there, and you can see it's now guessing in the range of

64
00:04:55,150 --> 00:05:03,550
501 to 1000. The next guess is 750. 1987
is higher, so the next guess, higher again. 875,

65
00:05:03,550 --> 00:05:08,590
well it still needs to be higher. 938, and
number's 987, so it still needs to be

66
00:05:08,590 --> 00:05:16,600
higher. 969, higher. 985 - we're getting
much closer now, but higher. At this point

67
00:05:16,600 --> 00:05:22,280
now, the computer's guessed 993. That's
higher than 987 so we go lower.

68
00:05:22,280 --> 00:05:27,960
The next guess you can see is 989 and we're
guessing in the range of 986 to 992.

69
00:05:27,960 --> 00:05:32,720
So scan through the output at this
stage. I'm just going to scroll up a little bit,

70
00:05:32,720 --> 00:05:35,000
and you can see all the hi-low ranges

71
00:05:35,000 --> 00:05:38,900
and see how they've changed each time. So
we need to go lower now because our

72
00:05:38,900 --> 00:05:44,420
number is 987, so I'm going to type l
there for lower, and you can see that

73
00:05:44,420 --> 00:05:48,410
the next guess is now the correct value.
So printing out information such as the

74
00:05:48,410 --> 00:05:52,460
low and high values can be a great way
to help understand what code's doing.

75
00:05:52,460 --> 00:05:55,720
It's often quicker than using the debugger,
and something you might want to try

76
00:05:55,730 --> 00:05:59,240
first, but with that said of course, the
debugger is always available if you want

77
00:05:59,240 --> 00:06:03,410
to check things more closely. Alright, so I'm
going to enter c there to say that the computer

78
00:06:03,410 --> 00:06:07,220
was correct, and you got it in nine guesses, as
you can see there. I'm just going to fix

79
00:06:07,220 --> 00:06:11,680
this run pane and then close it, and
we'll end the video here.

80
00:06:11,680 --> 00:06:16,040
In the next video I want to talk about something
called an Augmented assignment. Now this

81
00:06:16,040 --> 00:06:20,150
may be a suggestion that you see on line
27. I'm on Windows and it doesn't pop up

82
00:06:20,150 --> 00:06:25,670
for me, but you might be, if you're on a
Mac or on a Linux machine, you might be

83
00:06:25,670 --> 00:06:29,330
seeing a suggestion that the assignment
can be replaced with an Augmented

84
00:06:29,330 --> 00:06:33,170
assignment. And the suggestions do vary
and it may or may not appear. The bottom

85
00:06:33,170 --> 00:06:36,530
line is we're going to talk about that,
and find out what that's all about

86
00:06:36,530 --> 00:06:39,310
in the next video.

