1
00:00:04,240 --> 00:00:09,000
Alright, let's talk more about using if, elif
and else in the debugger.

2
00:00:09,000 --> 00:00:14,200
At the moment, our program only allows one guess as its answer. Now we're going to change that in an

3
00:00:14,200 --> 00:00:17,320
upcoming video,
but first, let's run it in the debugger

4
00:00:17,320 --> 00:00:22,280
just to confirm what's happening. We're
gonna set a breakpoint over here, on line 6.

5
00:00:22,280 --> 00:00:27,880
I'm going to right click and choose
Debug guessinggame. Now if you're using a

6
00:00:27,890 --> 00:00:31,789
different IDE, just start your debugger
in the usual way. Alright, so at the moment,

7
00:00:31,789 --> 00:00:35,390
you can see down the bottom of the screen,
our program's waiting for our input.

8
00:00:35,390 --> 00:00:39,470
So I'm going to click into here and enter
the answer 3, and that's obviously less than

9
00:00:39,470 --> 00:00:44,720
our answer 5. Now when I press enter, the
debugger stops on line 6. That's going to

10
00:00:44,720 --> 00:00:48,230
test 'if guess is less than the answer',
which of course it is. We can see the

11
00:00:48,230 --> 00:00:53,080
values of guess and answer in the
variables pane. Now IntelliJ also shows

12
00:00:53,080 --> 00:00:57,820
the values in the code pane. We can see
answer 5 at the end of line 1, but also,

13
00:00:57,820 --> 00:01:03,940
we can see 3 as the guess entered at the
end of line 4. So work out in your mind

14
00:01:03,940 --> 00:01:08,000
which line's going to be executed next,
before using the step-over button. So I'm

15
00:01:08,000 --> 00:01:15,040
going to come over and do that. As
expected, we get to line 7. Stepping over again,

16
00:01:15,040 --> 00:01:18,240
the program's executed, and did
you note that the little console

17
00:01:18,259 --> 00:01:22,130
tab has got the little yellow circle and
a down arrow, indicating there was some

18
00:01:22,130 --> 00:01:25,700
output. You might have seen that flashing,
so we click onto that. We can see we've

19
00:01:25,700 --> 00:01:29,300
now got some output below where we entered 3,
"Please guess higher", and the program's

20
00:01:29,300 --> 00:01:33,020
terminated.
So basically, Python won't test the code

21
00:01:33,020 --> 00:01:37,759
in an elif or an else when the if
condition is true. And obviously, in this

22
00:01:37,760 --> 00:01:42,640
case, guess < answer was True. It printed
"Please guess higher" and the program terminated.

23
00:01:42,640 --> 00:01:45,260
Alright, so now we're about
to test the condition guess is greater

24
00:01:45,260 --> 00:01:48,960
than answer, which will be true. So
let's rerun,

25
00:01:48,960 --> 00:01:55,180
or re-debug I should say, by clicking on debug. I'm going to enter 8 as my guess.

26
00:01:55,180 --> 00:01:57,780
We've stopped on line 6 again and
this time the condition is going to be

27
00:01:57,800 --> 00:02:02,320
false because 8 is obviously not less than 5.
So I'm going to step over.

28
00:02:02,320 --> 00:02:06,220
Next we're about to test the condition guess
greater than answer, which will be true.

29
00:02:06,220 --> 00:02:11,090
So I'm going to step over again, stepping
over one more time. You can see the flashing

30
00:02:11,090 --> 00:02:14,980
little down arrow in the console tab at
the top there. Click on that and we can

31
00:02:14,980 --> 00:02:18,680
see the output "Please guess lower".
Alright, so let's run the program one

32
00:02:18,680 --> 00:02:23,320
more time, in the debugger this time.
We're going to enter the guess as 5.

33
00:02:23,320 --> 00:02:27,110
Alright, so now that we've entered 5,
so the condition on line 6 will be

34
00:02:27,110 --> 00:02:31,000
false, and so will the condition on line
8. Those lines will still get

35
00:02:31,000 --> 00:02:35,940
executed. Python has to evaluate the
conditions to know what the results will be.

36
00:02:35,940 --> 00:02:37,940
It's the code in the blocks after the if

37
00:02:37,940 --> 00:02:42,980
and elif that will get skipped. So let's
use step over, to see the first

38
00:02:42,980 --> 00:02:47,810
condition getting evaluated, and then
we'll click step over again to evaluate

39
00:02:47,810 --> 00:02:52,520
line 8. Execution has jumped to
line 11. Note that the else isn't

40
00:02:52,520 --> 00:02:57,220
evaluated. There's no condition to evaluate.
else blocks will be executed if nothing

41
00:02:57,220 --> 00:03:03,520
else evaluated to true. The rules I
mentioned a couple of videos ago are simple.

42
00:03:03,520 --> 00:03:05,080
So an if-statement begins with the

43
00:03:05,100 --> 00:03:11,709
keyword if, followed by condition. The if
condition will always be evaluated.

44
00:03:11,709 --> 00:03:15,980
Next ,you can have one or more elif
blocks. You don't have to include elif

45
00:03:16,000 --> 00:03:21,160
and we will be seeing code that doesn't,
but if you have any elif lines, then they

46
00:03:21,160 --> 00:03:28,520
come after the if. elif also has to come
before else, if there is an else.

47
00:03:28,520 --> 00:03:32,840
And finally, you may have an else block.
You don't have to use else, but if you do,

48
00:03:32,840 --> 00:03:38,600
it must come after the if, and it must
also come after any elifs, if there are any.

49
00:03:38,660 --> 00:03:41,980
Alright so we're back in the code
here and I'm going to click on step over

50
00:03:41,980 --> 00:03:47,180
one more time. Program's ended, click on the console tab and you can see "You've got it first time"

51
00:03:47,180 --> 00:03:50,600
is outputted to the screen.
Alright, in the next video we're going to

52
00:03:50,600 --> 00:03:54,410
improve the program slightly. It's still
not going to be very exciting. We've got

53
00:03:54,410 --> 00:03:58,340
a few more things to learn first but we
have to learn to walk before we can run.

54
00:03:58,340 --> 00:04:01,520
See you in the next video.

