1
00:00:04,680 --> 00:00:09,330
So running our high-low game in the
debugger can be a bit slow. The loop is

2
00:00:09,330 --> 00:00:12,959
only going around 10 times, but if we had
a much larger range of numbers, you'd

3
00:00:12,959 --> 00:00:16,379
soon get pretty bored clicking the
Step Over button. When I ran the program

4
00:00:16,379 --> 00:00:21,860
in the last video, things really only got
interesting when the computer guessed 99.

5
00:00:21,860 --> 00:00:25,349
What would be really good would be if we
could get the debugger to stop at that

6
00:00:25,349 --> 00:00:32,159
point, when guess is 99. Luckily, we can do
just that. I'll show you, now, how to do

7
00:00:32,159 --> 00:00:36,329
that with both IntelliJ and PyCharms
debuggers. But if you're using a

8
00:00:36,329 --> 00:00:39,710
different IDE, your debugger may well
have a similar feature.

9
00:00:39,710 --> 00:00:44,070
Unfortunately, the idle debugger doesn't
have conditional breakpoints, which is

10
00:00:44,070 --> 00:00:47,520
what we need here. So if you're using
IDLE, you'll have to step over your code in

11
00:00:47,520 --> 00:00:51,879
the usual way.
So to achieve this, firstly, set a

12
00:00:51,879 --> 00:00:56,469
breakpoint on line eight, which is our
while loop, and we're not interested in

13
00:00:56,469 --> 00:01:00,100
seeing the first few lines being
executed. The debugger's going to stop each

14
00:01:00,100 --> 00:01:04,720
time the loop goes around, which isn't
really what we want. We're really only

15
00:01:04,720 --> 00:01:09,280
interested in what happens once low and
high are equal. So to set a condition on

16
00:01:09,280 --> 00:01:14,740
a breakpoint, right-click the breakpoint
dot in the margin. Now the condition that

17
00:01:14,740 --> 00:01:19,840
we want here, typing into this field,
is low is equal to high. So I'm going to

18
00:01:19,840 --> 00:01:24,850
type low equal high - remember to use two
equal signs, and I'm gonna click Done

19
00:01:24,850 --> 00:01:28,720
once I've done that. So notice that
there's now a little question mark next

20
00:01:28,720 --> 00:01:33,610
to the red dot, as well, to indicate that
it's a conditional breakpoint. So I'm gonna start

21
00:01:33,610 --> 00:01:36,840
the debugger, as we would normally do,

22
00:01:37,820 --> 00:01:42,320
and you saw me right click in the editor
pane and choose debug. I like to do that,

23
00:01:42,320 --> 00:01:45,280
as per normal, because that makes sure
that I'm debugging the correct file,

24
00:01:45,280 --> 00:01:48,360
because you might have a different file
selected up here. In which case, it would

25
00:01:48,360 --> 00:01:53,880
be invoking that file and not this one.
In any event, debugger test per normal,

26
00:01:53,880 --> 00:01:57,819
press ENTER to start. Alright so I'm
going to use the same number as we did

27
00:01:57,819 --> 00:02:02,440
in the last video - 100. So I need to
go through and let the computer pick,

28
00:02:02,440 --> 00:02:14,600
so it's gonna be lower -
I'll just go through and do this. So far, the

29
00:02:14,600 --> 00:02:20,260
breakpoint hasn't been hit yet, but it'll
do that as soon as I enter h now, to go

30
00:02:20,260 --> 00:02:27,370
higher. I do that, you can see what's
happened there. We stopped on line eight

31
00:02:27,370 --> 00:02:31,930
and low does equal high. We can see both
values there, in the variables pane at

32
00:02:31,930 --> 00:02:35,379
the bottom of the screen, for low and for
high. So that means the condition will

33
00:02:35,379 --> 00:02:39,129
now evaluate to false, and the while
loop will terminate. If we click Step

34
00:02:39,129 --> 00:02:44,560
Over, let's just check that that does
happen. You can see that did happen. Basically,

35
00:02:44,560 --> 00:02:48,519
because the while loop terminated normally,
we didn't break out of it. Execution is

36
00:02:48,519 --> 00:02:52,660
continuing in the else block. As you can
see, sitting on line 30. We switch to the

37
00:02:52,660 --> 00:02:58,500
console tab. If I click on resume program now,

38
00:02:58,560 --> 00:03:01,980
you see we've got the last two lines of output
there. So we should do that again, this

39
00:03:01,980 --> 00:03:05,850
time with a number that doesn't cause
low and high to become equal. So I'm

40
00:03:05,850 --> 00:03:11,580
going to set a breakpoint on the break
statement on line 23. Okay, I'm gonna start the

41
00:03:11,580 --> 00:03:21,270
debugger again, enter to start. This time
my number's going to be 250, so I'm gonna

42
00:03:21,270 --> 00:03:26,280
press Enter, which I've done, and I'm going to
lower. This time that answer is correct

43
00:03:26,280 --> 00:03:31,590
so I'm going to press c for correct, and
we've now hit the breakpoint, as you can

44
00:03:31,590 --> 00:03:35,250
see, so the debugger stops on our
breakpoint now, on line 23. We're about to

45
00:03:35,250 --> 00:03:40,650
break out of the loop. So that means that
the else block won't be executed. Go back

46
00:03:40,650 --> 00:03:48,380
to the console tab - we can see that the
computer got it in two guesses. So we use Step Over -

47
00:03:48,380 --> 00:03:51,780
that confirms that the else block
isn't executed. There's no more code and

48
00:03:51,780 --> 00:03:57,000
the program's terminated, as you can see.
Alright, so that's what else does when

49
00:03:57,000 --> 00:04:01,560
used with a loop. Because python reuses
the word else for this, it can be

50
00:04:01,560 --> 00:04:05,550
confusing. It would have been a lot
clearer if Python used something that,

51
00:04:05,550 --> 00:04:08,430
perhaps, made a bit more sense - 
something like, instead of else, perhaps

52
00:04:08,430 --> 00:04:16,839
completed. That would probably have made
a lot more sense, or perhaps, no break.

53
00:04:16,839 --> 00:04:20,120
But unfortunately, that's not the case.
It doesn't use either of those - it uses else -

54
00:04:20,130 --> 00:04:24,030
so I'll just undo both of those and go
back to what we had. But at this point,

55
00:04:24,030 --> 00:04:28,680
you do now know what else does when used
in a for or a while loop. You don't have

56
00:04:28,680 --> 00:04:31,890
to use it in your own code, but you may
come across it in code that you have to

57
00:04:31,890 --> 00:04:35,980
work with, and now hopefully, you've
got a good understanding of what it does.

58
00:04:35,980 --> 00:04:39,000
Alright, so in the next video, we're going
to see else used in an earlier example

59
00:04:39,000 --> 00:04:44,000
from this section.
See you in the next video.

