1
00:00:04,720 --> 00:00:09,910
In the last video, we saw how a binary
chop can be used, to reduce the number of

2
00:00:09,910 --> 00:00:15,160
guesses needed. In this video, we'll write
the code to let the computer guess a number.

3
00:00:15,160 --> 00:00:17,240
The code will implement the algorithm

4
00:00:17,240 --> 00:00:22,240
that we discussed in the slides, in the
last video. So an algorithm is a set of

5
00:00:22,240 --> 00:00:27,820
steps that are followed, to perform a
task or calculate a result. So I'm going

6
00:00:27,820 --> 00:00:31,539
to start with a couple of variables for
our low and high values. We need a new

7
00:00:31,540 --> 00:00:41,440
file for this, and we're going to call
this new Python file hilo.

8
00:00:41,440 --> 00:00:49,200
We're gonna take the opportunity to close some of
these other tabs. Alright, so the variables I'm

9
00:00:49,210 --> 00:00:55,750
starting with: low equals one and high
equals 1000. So next we need some simple

10
00:00:55,750 --> 00:01:00,340
instructions and get the program to wait
until we're ready. So on line 3, print

11
00:01:00,340 --> 00:01:12,120
parentheses double quotes Please think
of a number between,

12
00:01:12,120 --> 00:01:16,520
and we need our left and right curly braces and
another set of left and right curly braces,

13
00:01:16,520 --> 00:01:21,780
because obviously we're going to change that to
the actual numbers, dot format outside of

14
00:01:21,780 --> 00:01:27,740
the double quotes, low comma high in
parentheses and the closing parenthesis.

15
00:01:27,740 --> 00:01:33,300
Then we need to add a line to get
started, so import parentheses double quotes,

16
00:01:33,300 --> 00:01:39,660
press ENTER to start. So on line 5,
we're not interested in anything that

17
00:01:39,670 --> 00:01:43,479
the human types. We're just calling the
input function so that the computer

18
00:01:43,479 --> 00:01:47,840
waits until the user presses the enter
key to start the game.

19
00:01:47,840 --> 00:01:52,159
It will be useful to count the number of
guesses. We know that the computer should

20
00:01:52,159 --> 00:01:56,690
be able to get the correct answer with
ten guesses at most, and that's because

21
00:01:56,690 --> 00:02:03,000
we're having the range of numbers each
time, and two to the power of 10 is 1,024.

22
00:02:03,000 --> 00:02:07,180
So that's slightly more than 1,000. In
fact, the computer can guess any number

23
00:02:07,190 --> 00:02:13,550
from 1 to 1,023, within 10 guesses.
But 1,023 is a strange number to

24
00:02:13,550 --> 00:02:18,200
show to the user. That's why we're
sticking with 1,000. Alright so we need

25
00:02:18,200 --> 00:02:21,120
a variable now to store the
number of guesses,

26
00:02:21,120 --> 00:02:26,360
so on line 7, I'll type guesses equals 1.
Alright so we'll use a while loop to

27
00:02:26,360 --> 00:02:30,350
let the computer keep guessing until it
gets the answer. After each guess the

28
00:02:30,350 --> 00:02:34,610
human will tell the computer whether to
guess higher or lower, or if it got the

29
00:02:34,610 --> 00:02:38,959
answer right. We'll keep looping until
the answer is correct and break out of

30
00:02:38,960 --> 00:02:46,180
the loop when it is. So I'm going to do a
while True colon, then on line 9,

31
00:02:46,180 --> 00:02:55,800
guess equals low plus, and in parentheses, high take low,
integer division so two slashes there, 2.

32
00:02:55,800 --> 00:02:58,459
So we're dividing by 2 there and
making sure that we're using integer

33
00:02:58,460 --> 00:03:03,460
division. Now I mentioned while True in
an earlier video. True is always true,

34
00:03:03,460 --> 00:03:05,920
which means this loop will go round forever.

35
00:03:05,920 --> 00:03:09,340
Sometimes it's easier to do that then
break out of the loop at some point,

36
00:03:09,340 --> 00:03:12,720
rather than trying to include a
condition in the while loop. So what we

37
00:03:12,739 --> 00:03:16,970
want, is for the loop to stop when the
computer gets the right answer, but

38
00:03:16,970 --> 00:03:21,709
because the computer doesn't know the
answer, we can't include a test here. So

39
00:03:21,709 --> 00:03:26,570
what I mean is, on line 8 here, we can't
do something like wild guess is not

40
00:03:26,570 --> 00:03:31,549
equal to answer, because we don't actually
know the value of answer. So I'm going to

41
00:03:31,549 --> 00:03:35,359
do that change and go back to what we
had, while True, so that the program will

42
00:03:35,359 --> 00:03:39,769
keep looping. The line 9 is the formula
that we saw in the slides in the

43
00:03:39,769 --> 00:03:44,359
previous video. It calculates the
midpoint between the low and high values

44
00:03:44,359 --> 00:03:49,370
to produce the next guess. So what we'll
do is we'll print the guess, then ask the

45
00:03:49,370 --> 00:03:53,480
user to type H if the computer should guess
higher, L if the computer should guess

46
00:03:53,480 --> 00:03:58,489
slower or C if the guess was correct. And
we can include the text that we want to

47
00:03:58,489 --> 00:04:02,840
print in the input prompt. So I'm going to come down here, line 10,

48
00:04:02,840 --> 00:04:12,640
high underscore low is equal to
input parentheses double quotes My guess

49
00:04:12,640 --> 00:04:17,930
is and left and right curly braces.
Full stop. Should I guess

50
00:04:17,930 --> 00:04:32,440
higher or lower? - should I type better as well.
Should I guess higher or lower? Enter h

51
00:04:32,440 --> 00:04:40,820
or l, or c if my guess was correct, and then
double quotes, and we can't really see

52
00:04:40,820 --> 00:04:44,580
now and I'm going to actually talk about
having lines that are too long, in a minute.

53
00:04:44,580 --> 00:04:49,700
We're gonna keep typing for now and
put dot format left parenthesis guess

54
00:04:49,700 --> 00:04:55,300
two right parentheses, then outside of that I'm
going to do dot casefold. So again, as I

55
00:04:55,310 --> 00:04:59,030
pointed out there, that line is a bit
long. So you can easily see here the

56
00:04:59,030 --> 00:05:01,820
problem that long lines can cause,
because it's hard for me to show it all

57
00:05:01,820 --> 00:05:06,060
on the video. In fact, I can't show the
entire line on the video right now.

58
00:05:06,060 --> 00:05:09,500
I don't want to digress here, but I will be
talking about line length and other

59
00:05:09,500 --> 00:05:13,280
things about formatting a code in a
later video. Remember this example and

60
00:05:13,280 --> 00:05:17,810
how inconvenient it is for you to type
the code from my screen, when we come

61
00:05:17,810 --> 00:05:21,409
back to code style, noting that you can't
see it all at once. For now, though, what I'm

62
00:05:21,409 --> 00:05:24,979
going to do is split this line. So I'm
going to position the cursor before

63
00:05:24,980 --> 00:05:30,600
the E of enter. I'm going to type a double
quote, then I'm going to press Enter.

64
00:05:30,600 --> 00:05:34,340
That gives an error, as you can see, but
that's okay, just enter another double

65
00:05:34,340 --> 00:05:38,990
quote here and that then delimits the
split-up string correctly. If I go back

66
00:05:38,990 --> 00:05:42,860
now, we can see the whole thing on the
screen. The double quote should line up

67
00:05:42,860 --> 00:05:46,550
with the one on the line above but don't
worry too much about that just yet. We'll

68
00:05:46,550 --> 00:05:49,699
leave it as it is for now. What we will
do is position the cursor again before the

69
00:05:49,699 --> 00:05:55,849
dot of format, up here before the dot,
press ENTER again. That's now split the

70
00:05:55,849 --> 00:05:59,840
line into three. Now rather than lining
up manually, what you could do is go to

71
00:05:59,840 --> 00:06:07,060
the code menu and choose Reformat code.
Most modern IDEs have a similar feature,

72
00:06:07,060 --> 00:06:11,000
and now the code is correctly formatted,
as you can see. Alright so I think you'd

73
00:06:11,000 --> 00:06:14,810
agree now, that's easier to read. You can
now see all the code on my screen and I

74
00:06:14,810 --> 00:06:17,240
don't have to scroll
sideways as much to show it to you.

75
00:06:17,240 --> 00:06:20,680
Alright so moving on with this program,
we've got four possibilities to deal

76
00:06:20,680 --> 00:06:26,840
with. The user could enter h for a
high guess, so let's start with that.

77
00:06:26,840 --> 00:06:35,980
So I'll start that on line 14.
If high underscore low is equal to

78
00:06:35,980 --> 00:06:40,100
h in lowercase : Remember we're using case
fold on line 12,

79
00:06:40,100 --> 00:06:43,760
so we're guaranteed it's going to be in
lowercase. And the comment here, we want

80
00:06:43,760 --> 00:06:56,680
to Guess higher. The low end of the range
becomes 1 greater than the guess.

81
00:06:56,680 --> 00:06:59,680
Now I'm not going to add the code for
guessing higher. Let's get all the

82
00:06:59,690 --> 00:07:03,440
conditions in place first. That'll let
you see what the conditions are, but it

83
00:07:03,440 --> 00:07:06,950
will cause a problem. Because the error
message is very confusing, I want to get

84
00:07:06,950 --> 00:07:12,020
the error and explain it. Right, so moving
on now, the next condition to test is if

85
00:07:12,020 --> 00:07:16,920
the user enters l for lower. So we're
going to go ahead and do that on line 17:

86
00:07:16,920 --> 00:07:23,540
elif high_low is equal to l :
again in lower case, and a comment

87
00:07:23,540 --> 00:07:28,580
for that will be Guess lower. The high end of
the range becomes 1 less than the guess.

88
00:07:34,000 --> 00:07:36,940
So we're getting errors here but we'll
ignore them for the moment.

89
00:07:36,940 --> 00:07:40,460
Next the input would be c if the guess
is correct, so let's move down on to that,

90
00:07:40,460 --> 00:07:47,780
line 20, elif high_low is
equal to c in double quotes, lower case c :

91
00:07:47,780 --> 00:07:51,140
So what I'll do here is print out the answer
here, no need to add a comment, so print

92
00:07:51,140 --> 00:07:56,720
parentheses double quotes I got it
in, left and right curly braces

93
00:07:56,720 --> 00:08:03,950
guesses exclamation mark double quote dot
format guesses. Alright we'll scroll up a

94
00:08:03,950 --> 00:08:07,670
little bit so we can see the other code.
Alright so that's the three conditions

95
00:08:07,670 --> 00:08:11,690
that we expect, but humans often don't do
what we expect, and we should also deal

96
00:08:11,690 --> 00:08:15,700
with anything else that they might type.
We're gonna use an else block to do that.

97
00:08:15,700 --> 00:08:27,920
I'm gonna come down here, on line 23, else colon
print parentheses Please enter h, l or c.

98
00:08:27,920 --> 00:08:29,500
So if they do anything else, we just remind

99
00:08:29,500 --> 00:08:32,349
them what the three options are. Alright,
so I'm going to stop the video here and

100
00:08:32,349 --> 00:08:35,740
in the next one, we'll look at why we've
got those errors.

101
00:08:35,740 --> 00:08:38,820
See you in the next video.

