1
00:00:04,500 --> 00:00:08,890
Sometimes we need to check more than
just two cases. So looking at the code

2
00:00:08,890 --> 00:00:14,420
from the last video, we're testing to see
if age is greater than equal to 18 on line 5,

3
00:00:14,420 --> 00:00:19,780
and we test the opposite condition,
age less than 18, on line 11.

4
00:00:19,820 --> 00:00:24,420
In both cases, the condition can be either
True or False. If it's True,

5
00:00:24,420 --> 00:00:31,520
the code blocks on lines 6 and 7 and line 12 are executed.
If the condition evaluates to False,

6
00:00:31,520 --> 00:00:36,480
the code in the else block executes.
Python lets us test several conditions

7
00:00:36,480 --> 00:00:41,340
by using elif, so I'll explain a couple
of rules about elif in a moment.

8
00:00:41,340 --> 00:00:45,640
Firstly though, let's see an example of using it.
What I'm going to do is add another condition

9
00:00:45,640 --> 00:00:52,180
to the second if statement. So I'm gonna come
down here to line 12, after line 12,

10
00:00:52,180 --> 00:00:59,160
you could do a backspace there. I'm going to
type elif space age is equal to, that's two

11
00:00:59,160 --> 00:01:05,640
equal signs 900: and on the indented line I'm gonna type print,

12
00:01:05,640 --> 00:01:16,880
in parentheses double quotes,
Sorry, Yoda you die in Return of the Jedi,

13
00:01:16,880 --> 00:01:23,840
and I've left the final else on line 15.
So elif, if you haven't guessed it, is short for else if.

14
00:01:23,860 --> 00:01:28,520
The code now checks to see if age is
less than 18. If it is, the message on

15
00:01:28,539 --> 00:01:33,399
line 12 will be printed. If age isn't
less than 18, Python checks the elif

16
00:01:33,400 --> 00:01:40,020
condition on line 13. So that'll evaluate
to either True or False. If True,

17
00:01:40,020 --> 00:01:43,660
we let Yoda know of his demise. We're assuming
that anyone who's 900 years old must

18
00:01:43,660 --> 00:01:48,620
be Yoda from Star Wars, and if I just
spoiled that movie for you I do apologize.

19
00:01:48,620 --> 00:01:51,640
But moving on, if the age
doesn't equal 900,

20
00:01:51,640 --> 00:01:58,100
the condition evaluates to False and Python
continues with the else on line 15.

21
00:01:58,100 --> 00:02:01,740
Now before I run the program, what I'm going
to do is comment out the first if

22
00:02:01,749 --> 00:02:05,380
statement on lines 5 through 9, and
instead of having to manually go through

23
00:02:05,380 --> 00:02:09,490
each line, most IDEs do have a quick way
of doing that. So in IntelliJ I can just

24
00:02:09,490 --> 00:02:13,460
select the lines I want to comment out.
I'm on Windows, I can do a control

25
00:02:13,460 --> 00:02:18,680
forward slash,
and it's a Cmd / if you're on a Mac

26
00:02:18,680 --> 00:02:23,500
and the Ctrl slash will also work on
Linux. Alright, so that's commented out

27
00:02:23,500 --> 00:02:28,620
and now won't execute. Let's run the
program, and first, I'm going to enter Tim

28
00:02:28,629 --> 00:02:34,200
as my name again, which I've been doing.
Then next we have to enter an age.

29
00:02:34,200 --> 00:02:39,000
We've got a few tests to perform, starting with
the value less than 18, so let's try that

30
00:02:39,000 --> 00:02:44,540
and I'll do 11, and I'll just put the code
on screen there, so we can see that.

31
00:02:44,540 --> 00:02:49,500
So you can see that's less than 18 that I entered,
11, and the code on line 12 was executed,

32
00:02:49,500 --> 00:02:52,340
and we got that output, and
that's because the condition on line 11

33
00:02:52,359 --> 00:02:56,319
evaluated to a true, and just to be
consistent let's check what happens when

34
00:02:56,320 --> 00:03:04,200
I enter an age of exactly 18. Tim and
18, 18 You're old enough to vote.

35
00:03:04,200 --> 00:03:08,100
Please put an X in the box, and
this time the condition on line 11

36
00:03:08,100 --> 00:03:14,500
evaluated to False. Python then checks
the elif condition, the else if, on line 13.

37
00:03:14,500 --> 00:03:18,960
This also evaluates to False and
therefore, line 14 didn't get executed.

38
00:03:18,960 --> 00:03:23,040
Instead Python skipped to the else block.
There's no more conditions to check and

39
00:03:23,080 --> 00:03:26,720
none of the ones that were checked were
True, so we're left with the else.

40
00:03:26,720 --> 00:03:31,140
Lines 16 and 17 were executed and we get that
message that You're old enough to vote,

41
00:03:31,140 --> 00:03:35,280
and to put an X in the box. And just to
confirm, if we run this again,

42
00:03:35,280 --> 00:03:41,660
if I enter an age that's greater than 18, you should
see the same result. So I put, say 45.

43
00:03:41,660 --> 00:03:46,080
Same result as we just talked about,
when I entered 18.

44
00:03:46,080 --> 00:03:54,480
Alright, so the final test now is to enter 900 to see
whether that works, and you can see there 900,

45
00:03:54,489 --> 00:03:58,449
Sorry Yoda you die in Return of the Jedi.
So we're getting that message and

46
00:03:58,449 --> 00:04:03,760
that's because the elif, else if, for age
is equal to 900, evaluates to True,

47
00:04:03,760 --> 00:04:08,079
and line 14 was executed. So all that would
be much easier to understand if we could

48
00:04:08,080 --> 00:04:13,240
see the computer executing the code, line
by line. Well as it happens we can do that.

49
00:04:13,240 --> 00:04:15,980
In the next couple of videos we're
going to have a look at the debugger

50
00:04:15,980 --> 00:04:20,228
that's built into the IntelliJ IDEs.
We're going to look at the IntelliJ and

51
00:04:20,228 --> 00:04:24,969
PyCharm debugger in the next video. In
the video after that we'll see how to do

52
00:04:24,969 --> 00:04:28,029
the same thing with the debugger that's
built into IDLE. Now if you're using

53
00:04:28,029 --> 00:04:31,340
a different IDE, it probably
has a debugger as well,

54
00:04:31,340 --> 00:04:35,520
but please don't ask us how to use it.
There's literally hundreds of IDEs,

55
00:04:35,520 --> 00:04:38,960
and we've chosen to use the Jetbrains products for this course.

56
00:04:38,960 --> 00:04:42,220
If you use something else, then that's
either because you know how to use it,

57
00:04:42,220 --> 00:04:45,340
or there's someone in your company who does
know. We can't, unfortunately, provide

58
00:04:45,340 --> 00:04:48,640
support for other IDEs, but if it's
IntelliJ support you're after, were quite

59
00:04:48,640 --> 00:04:50,820
happy to help you with that.
So let's move on now,

60
00:04:50,820 --> 00:04:54,140
and I'll see you in the next video.

