1
00:00:04,660 --> 00:00:10,060
So there's one other thing we can do with loops,
and that's to include an else clause.

2
00:00:10,060 --> 00:00:14,080
Experienced programmers often
avoid using else in this way, and that's

3
00:00:14,080 --> 00:00:18,200
because less experienced programmers can
struggle to understand their code.

4
00:00:18,200 --> 00:00:22,120
But it's something you might come across,
so I want to cover it here. We've seen else

5
00:00:22,130 --> 00:00:26,179
when we looked at testing conditions
using if, but it can also be used at the

6
00:00:26,179 --> 00:00:30,650
end of loops. It's possibly an
unfortunate word to use for what it does

7
00:00:30,650 --> 00:00:35,900
with loops though, because it's use
doesn't imply "otherwise" in any sense.

8
00:00:35,900 --> 00:00:40,730
What it does, is cause a block of code to
be executed if the loop was allowed to

9
00:00:40,730 --> 00:00:45,140
continue to the end, in other words, if
the loop wasn't broken out of. With what

10
00:00:45,140 --> 00:00:49,070
we've covered so far, any example we use
will either be contrived, or could be

11
00:00:49,070 --> 00:00:52,399
written better some other way. I don't
like showing you code that could be

12
00:00:52,399 --> 00:00:57,230
written better, so the first example will
be a bit contrived. So let's assume we've

13
00:00:57,230 --> 00:01:02,000
noticed that lottery numbers divisible
by 8 are less likely to appear. Now I

14
00:01:02,000 --> 00:01:06,860
know that's not realistic - I did say this
example was contrived. We want to write

15
00:01:06,860 --> 00:01:11,210
some code that will check a list of
numbers, and let us know if the list

16
00:01:11,210 --> 00:01:18,060
contains any values that are divisible
by 8. So let's start with a new file.

17
00:01:18,060 --> 00:01:32,140
We'll call this one contrived.py, and we'll
start with a new list as well:

18
00:01:32,140 --> 00:01:38,719
numbers is equal to left and right
square brackets 1 comma 45 comma 32

19
00:01:38,719 --> 00:01:46,969
comma 12 and 60. Now we're going to look
at lists in the next section. All we need

20
00:01:46,969 --> 00:01:52,020
to know here, is that they're enclosed in
square brackets and are a sequence type.

21
00:01:52,020 --> 00:01:54,979
So we know that we can iterate over
sequences - we did that a lot with the

22
00:01:54,980 --> 00:02:02,760
string type. Let's try out some code. So
I'm going to type for number in numbers:

23
00:02:02,760 --> 00:02:12,080
if number percent, which is the
remainder operator, 8 is equal to 0 : and I'll

24
00:02:12,080 --> 00:02:15,700
put a comment here - I'm going to put reject the list.

25
00:02:17,180 --> 00:02:22,519
Then I'll print out the message, print
parentheses double quotes The numbers

26
00:02:22,519 --> 00:02:32,819
are unacceptable, and we'll also break. So
here what we're doing, for each number in

27
00:02:32,819 --> 00:02:36,930
the list, we're checking if it's exactly
divisible by 8, using a modulo or

28
00:02:36,930 --> 00:02:41,010
remainder operator. If it is, we print a
message rejecting the list and break

29
00:02:41,010 --> 00:02:45,060
out of the loop. Now these numbers should
be rejected because we've got the value

30
00:02:45,060 --> 00:02:52,160
32 in the list. So we run the program and
the numbers are rejected, so that works.

31
00:02:52,160 --> 00:02:56,099
But let's see what happens with the list
of numbers that aren't divisible by 8.

32
00:02:56,099 --> 00:03:05,010
So I'm going to change the 32 to a 31. Run
the code again, and you see this time, we

33
00:03:05,010 --> 00:03:08,220
don't get the message that they're
unacceptable, which we're expecting, having

34
00:03:08,220 --> 00:03:13,460
changed 32 back to 31. So the code works
but it's not very friendly.

35
00:03:13,460 --> 00:03:17,700
It hasn't given us any output. So what
would be a lot friendlier, is if we've

36
00:03:17,700 --> 00:03:21,419
got a message saying that these numbers
were okay. So this is where Pythons else

37
00:03:21,419 --> 00:03:25,829
can be useful, when used in a loop. When
our loop finds a value that's not

38
00:03:25,829 --> 00:03:29,400
acceptable, our code breaks out of the
loop, as we saw the first time we ran the

39
00:03:29,400 --> 00:03:31,680
program.
That means the loop will only terminate

40
00:03:31,680 --> 00:03:36,209
normally if all the values are acceptable.
So what we want to do, is run

41
00:03:36,209 --> 00:03:40,019
some code when the loop terminates
normally. We don't want that code to

42
00:03:40,019 --> 00:03:43,829
execute if we break out of the loop, only
when it goes all the way around, and

43
00:03:43,829 --> 00:03:50,220
that's exactly what else does. So let's
see that working. So I'm going to put an

44
00:03:50,220 --> 00:03:56,940
else here on line 8, colon and print
parentheses double quotes All those

45
00:03:56,940 --> 00:04:04,060
numbers are fine. So if I run the program,

46
00:04:04,060 --> 00:04:08,040
this time we get the message, All those
numbers are fine. We didn't break out of the

47
00:04:08,040 --> 00:04:11,760
loop, in other words, and the code in the
else block is executed. Then when I

48
00:04:11,760 --> 00:04:17,760
change to this, to include an
unacceptable number, we'll change 12 to 16,

49
00:04:17,760 --> 00:04:24,000
run that again. This time we get the
output, The numbers are unacceptable.

50
00:04:24,000 --> 00:04:26,670
In other words, we've broken out of the
loop, and because the loop didn't

51
00:04:26,670 --> 00:04:30,930
terminate normally, the code in the else
block - this is the code on line 9 - didn't

52
00:04:30,930 --> 00:04:35,940
get executed. Now once again, take careful
notice of the indentation. This is

53
00:04:35,940 --> 00:04:42,400
particularly tricky, here as an else can
follow both the if or the for statements.

54
00:04:42,400 --> 00:04:47,360
It's only the indentation level that
ties the else to the for. Here, the else

55
00:04:47,370 --> 00:04:51,480
on line 8, is at the same level as the for, on line 3.

56
00:04:51,480 --> 00:04:56,820
That means else is associated with the
for loop and not with the if statement.

57
00:04:56,820 --> 00:05:00,320
Alright so that was a contrived example
to show how else works when it's used

58
00:05:00,330 --> 00:05:04,650
with a loop. You may want to step through
the code in the debugger, to see exactly

59
00:05:04,650 --> 00:05:09,480
what's happening. If you do, remember to
change 16 back to 12 and debug the code

60
00:05:09,480 --> 00:05:13,410
again, and that'll let you see the else
block executing, when the loop terminates

61
00:05:13,410 --> 00:05:16,830
normally. Alright so moving on,
in the next video

62
00:05:16,830 --> 00:05:22,790
we'll see a more realistic use of else.
See you in the next video.

