1
00:00:04,569 --> 00:00:09,530
For this video, we're going to make a
change to our high-low game. So open up

2
00:00:09,530 --> 00:00:15,290
hilo.py, and that's the one that we
used in an earlier video. Now we can use

3
00:00:15,290 --> 00:00:19,970
an else clause with our while loop, and
there's our while loop, to make the game

4
00:00:19,970 --> 00:00:24,740
a bit more impressive. Now let's have a
look at what we're going to do.

5
00:00:24,740 --> 00:00:29,000
Alright, so in the binary search slides, earlier,
we worked through what happens when we

6
00:00:29,000 --> 00:00:32,630
try to guess the number 7. We were
guessing in the range of 1 to 10. So I

7
00:00:32,630 --> 00:00:37,400
want to quickly review those earlier
slides. We start off by guessing the

8
00:00:37,400 --> 00:00:42,380
mid-point between the low and high values.
Our first guess is 5. We're told to guess

9
00:00:42,380 --> 00:00:46,550
higher - that means the answer must lie
somewhere between 6 and 10. The new

10
00:00:46,550 --> 00:00:51,080
mid-point is 8, which becomes our
second guess. We're told to guess lower -

11
00:00:51,080 --> 00:00:54,590
that means the answer must lie somewhere
between 6 and 7. Our third guess

12
00:00:54,590 --> 00:01:00,230
will be 6. Where things get interesting,
is in the next slide. 7 is greater than

13
00:01:00,230 --> 00:01:06,100
6, so we're told to guess higher.
The low value becomes equal to the high value.

14
00:01:06,100 --> 00:01:10,100
When we went through this example
earlier, I said at this point, a human

15
00:01:10,100 --> 00:01:14,750
would guess 7 right away. I also said
that the computer has to do the

16
00:01:14,750 --> 00:01:18,770
calculation first, but that's not
completely true. Rather than use our

17
00:01:18,770 --> 00:01:23,060
formula to calculate the new mid-point,
the computer could test to see if low

18
00:01:23,060 --> 00:01:28,330
is equal to high.
If low does equal high, the computer

19
00:01:28,330 --> 00:01:32,590
then doesn't have to ask us if the guess
is correct. When low and high are equal. it

20
00:01:32,590 --> 00:01:36,700
must have been the correct number. Note
that this is only the case because all

21
00:01:36,700 --> 00:01:41,320
numbers are present. In a normal binary
search, when what we're looking for may

22
00:01:41,320 --> 00:01:47,140
not be present, this doesn't apply. We can
make our program a bit more impressive,

23
00:01:47,140 --> 00:01:52,060
by not asking if the value is correct. We
know it must be, when low is equal to

24
00:01:52,060 --> 00:01:56,020
high, and we can tell the player that
we've guessed their number. And the

25
00:01:56,020 --> 00:01:59,200
changes to our code are quite simple. So
let's swing over and get started with

26
00:01:59,200 --> 00:02:05,620
that. Alright, so on line 23, we break out of
the loop when the player confirms that

27
00:02:05,620 --> 00:02:09,190
we've guessed the right number. In the
cases where we know we've got the right

28
00:02:09,190 --> 00:02:13,510
number, we can cause the loop to
terminate and use an else clause to tell

29
00:02:13,510 --> 00:02:17,140
the player what their number was. So
we're gonna start by changing the

30
00:02:17,140 --> 00:02:22,420
condition for the while loop. Let's go up
and do that. So instead of while true,

31
00:02:22,420 --> 00:02:30,480
we're gonna put while low is not equal
to high. So we continue looping, as long

32
00:02:30,489 --> 00:02:35,080
as low doesn't equal high. When they are
equal, the loop will terminate, and if

33
00:02:35,080 --> 00:02:38,620
that happens, and it won't happen for all
numbers - just some of them - but when that

34
00:02:38,620 --> 00:02:42,280
happens, we know that we've found the
correct number. So let's add the code for

35
00:02:42,280 --> 00:02:49,960
that. So there's going to be else down here
on line 29, else : line 30 print parentheses

36
00:02:49,960 --> 00:02:54,680
double quotes, You thought of the number,

37
00:02:54,989 --> 00:03:01,630
left and right curly braces double quote
dot format low in parentheses, and the

38
00:03:01,630 --> 00:03:07,630
right parenthesis to end the line. Then on
the next line, print, I got it in, and left

39
00:03:07,630 --> 00:03:14,980
and right curly braces again, guesses, double
quotes dot format guesses and two

40
00:03:14,980 --> 00:03:19,720
right parentheses. Now just before I run
the program, I'm going to comment out up

41
00:03:19,720 --> 00:03:25,520
here, the diagnostic information that
we're printing on line 9. So let's do that.

42
00:03:25,520 --> 00:03:28,600
Alright, so let's now run the program.

43
00:03:29,300 --> 00:03:33,519
So my number's going to be a hundred.
Press ENTER to start. So with my number being

44
00:03:33,520 --> 00:03:37,760
a hundred, the first guess is obviously
too high, so we're going to put lower

45
00:03:37,760 --> 00:03:49,540
lower,
lower, higher, higher, lower, lower, higher.

46
00:03:49,540 --> 00:03:54,470
And as you can see, the last guess there is 99.
If we tell the computer to go higher, it

47
00:03:54,470 --> 00:03:57,380
doesn't have to ask us if a hundred is
correct, because you can see it's already

48
00:03:57,380 --> 00:04:02,880
guessed 101, and now it's guessing 99. So
if I press h here, which is correct

49
00:04:02,880 --> 00:04:07,940
because it's higher than 99, You thought
of the number 100. I got it in 10 guesses.

50
00:04:07,940 --> 00:04:12,040
So at that point, when I selected that,
low and high both equalled a hundred, and

51
00:04:12,040 --> 00:04:18,040
the while loop terminated. It terminated
normally and the else clause was invoked,

52
00:04:18,040 --> 00:04:22,740
and we got the output on the screen, as you can see.

53
00:04:22,740 --> 00:04:26,759
So that's a bit more impressive. What
would be really good is if we could get

54
00:04:26,759 --> 00:04:31,169
the computer to do that for any guess,
but we can't. It only knows it has the

55
00:04:31,169 --> 00:04:36,020
correct answer when high and low converge
to the same value. With a high value

56
00:04:36,020 --> 00:04:40,740
that's close to an exact power of two,
that happens about half the time. 1000 is

57
00:04:40,740 --> 00:04:44,310
closer to 1024, and the
computer can tell it has the correct

58
00:04:44,310 --> 00:04:50,250
answer for 489 numbers that we think of.
If you'd like to run this code in the

59
00:04:50,250 --> 00:04:53,250
debugger, to help understand what's
happening, you'll find it a bit

60
00:04:53,250 --> 00:04:57,780
frustrating. It can be annoying debugging
code that waits for input. Setting

61
00:04:57,780 --> 00:05:01,530
breakpoints can help a bit, but there's
another technique you can use and I'll

62
00:05:01,530 --> 00:05:05,419
show you what that is in the next video.

