1
00:00:04,360 --> 00:00:08,500
In an earlier video in this section, we
had an example of a while loop in

2
00:00:08,500 --> 00:00:12,040
adventure.py. So if we open that file
in the editor, if you haven't already

3
00:00:12,040 --> 00:00:18,099
done so, let's review the problem we had.
So if we run the program now, and type

4
00:00:18,099 --> 00:00:23,739
quit to a break out of the loop, we get
the output Game over which is what we

5
00:00:23,739 --> 00:00:28,029
want, but the program also prints aren't
you glad you got out of there, as well. So

6
00:00:28,029 --> 00:00:31,000
we don't really want to print that
message if the player quits out of the

7
00:00:31,000 --> 00:00:34,180
game. Now you should be able to see that
fixing that problem is going to be

8
00:00:34,180 --> 00:00:38,800
pretty easy. If we break out of the while
loop, else can be used to prevent that

9
00:00:38,800 --> 00:00:42,640
message from happening. So all we really
need to do is add an else here on line

10
00:00:42,640 --> 00:00:48,789
10 : enter, and making sure that the
print is now indented. So the code in the

11
00:00:48,789 --> 00:00:53,379
else block will now only be executed if
the loop terminates normally. It won't be

12
00:00:53,379 --> 00:00:58,989
executed if we break out of the loop. So
let's run the program to test that. Let's

13
00:00:58,989 --> 00:01:03,010
start off by entering up and the loop
goes round again. If we enter quit at

14
00:01:03,010 --> 00:01:07,570
this point, we break out of the loop and
you can see we've got the message Game

15
00:01:07,570 --> 00:01:12,760
over but not the message on line 11. We
should also test what happens when the

16
00:01:12,760 --> 00:01:16,120
loop terminates normally so let's run
the program again, and this time we're

17
00:01:16,120 --> 00:01:23,380
going to enter a direction - I'm gonna
enter east - and now, you can see, we've

18
00:01:23,380 --> 00:01:27,040
got the message aren't you glad you got
out of there showing, which is correct.

19
00:01:27,040 --> 00:01:30,730
Now there are other ways to achieve the
same thing. You don't have to use else

20
00:01:30,730 --> 00:01:34,960
with a loop, but it can be useful and you
will see it being used in code out there.

21
00:01:34,960 --> 00:01:38,830
Alright so that's actually the end of
this section. In the next section, we're

22
00:01:38,830 --> 00:01:42,340
going to look at some data structures
that are built into Python. Before that

23
00:01:42,340 --> 00:01:45,660
though, I want to finish this section
with a brief summary and a challenge.

24
00:01:45,660 --> 00:01:49,220
So I'll see you in the next video.

