1
00:00:04,680 --> 00:00:09,180
Alright, so continuing on from the last
video, make sure you've still got the while

2
00:00:09,180 --> 00:00:13,289
loop.py program open in your
editor. So we've got a while loop on

3
00:00:13,289 --> 00:00:17,400
lines five through three seven, and you can see
that it's functionally identical to the

4
00:00:17,400 --> 00:00:21,599
for loop that I commented out on the
lines one and two. If you've programmed

5
00:00:21,600 --> 00:00:26,720
in C or Java, that while loop is how a
for loop really works in those languages.

6
00:00:26,720 --> 00:00:31,410
They both compile to the same code.
That's not true in Python. Although we're

7
00:00:31,410 --> 00:00:35,190
getting the same result, the two loops
are doing very different things behind

8
00:00:35,190 --> 00:00:39,690
the scenes. I mention that because you
wouldn't use a while loop just to iterate

9
00:00:39,690 --> 00:00:43,350
through a range of values, but it did
provide a good example to see what's

10
00:00:43,350 --> 00:00:47,040
going on. Before we look at more
realistic examples of while loops,

11
00:00:47,040 --> 00:00:50,520
there's a couple of things I want to say
about this one. We've seen that the

12
00:00:50,520 --> 00:00:54,570
condition i < 10 controls
execution of the block of code in the

13
00:00:54,570 --> 00:00:59,400
loop. As long as i is less than 10 the
code will be executed. Once i becomes

14
00:00:59,400 --> 00:01:03,030
equal to or greater than 10, the
condition becomes False and the loop

15
00:01:03,030 --> 00:01:08,430
terminates. So that leads to two other
important things to consider. First, the

16
00:01:08,430 --> 00:01:13,140
condition must be initialized before the
loop starts. With the for loop, we just

17
00:01:13,140 --> 00:01:17,760
used i without setting a value first.
Python knows to give it the first value

18
00:01:17,760 --> 00:01:22,260
from the range and continue from there.
With the while loop though, we have to

19
00:01:22,260 --> 00:01:27,450
give i a value first and we do that on
line 4. If we hadn't, we'd get an error

20
00:01:27,450 --> 00:01:31,770
that i is not defined. The second thing
is that we need some way within the loop,

21
00:01:31,770 --> 00:01:37,780
for the condition to become False. So
remove the indentation, as I'm doing here,

22
00:01:37,780 --> 00:01:44,240
before i plus 1, then we'll run the
program again. We run the program.

23
00:01:45,360 --> 00:01:48,900
Now because we're not increasing i
inside the loop, the condition never

24
00:01:48,900 --> 00:01:53,700
becomes False and the loop will keep
running forever. And we need to go up to

25
00:01:53,700 --> 00:01:59,180
the run menu in IntelliJ and choose stop to
stop this.

26
00:01:59,180 --> 00:02:03,260
I could have clicked on the little red button
to the left, as well. Now if you're using idle,

27
00:02:03,270 --> 00:02:07,530
control C will stop the program as well.
When we stopped incrementing i in the

28
00:02:07,530 --> 00:02:11,580
loop, there's no way for the condition to
become False, so the loop just kept going,

29
00:02:11,580 --> 00:02:15,090
as you saw. We could just as well,
in that scenario, have written

30
00:02:15,090 --> 00:02:18,680
the loop as follows:

31
00:02:20,380 --> 00:02:25,160
while True, as you can see there, and that
would get us the same undesirable

32
00:02:25,160 --> 00:02:28,940
situation. In some of our other examples,
you will see loops that just use

33
00:02:28,940 --> 00:02:32,360
True as the condition, but the body of
the loop will then contain a way to

34
00:02:32,360 --> 00:02:36,260
break out of the loop. It's easier to see
why our for loop is preferable in this

35
00:02:36,260 --> 00:02:41,450
case. The codes on lines 1 & 2 are easier
to understand and there's less chance of

36
00:02:41,450 --> 00:02:44,959
introducing an error. There are many
reasons for looping around a block of

37
00:02:44,960 --> 00:02:48,500
code that don't involve just carry
through items in a sequence. For example,

38
00:02:48,500 --> 00:02:52,820
in an adventure game, players have to
choose a direction in order to exit from

39
00:02:52,820 --> 00:02:56,480
a location. If they don't choose one of
the available exits, they stay where they

40
00:02:56,480 --> 00:03:03,580
are, and that's an ideal use of a while
loop. So lets create a new file here.

41
00:03:03,580 --> 00:03:14,260
We're gonna call this one adventure.py.
Alright, we're gonna start by typing available

42
00:03:14,260 --> 00:03:21,080
underscore exits is equal to square brackets
and in double quotes, north,

43
00:03:21,080 --> 00:03:25,310
double quote comma. We're going to do
south, east and west all in double quotes,

44
00:03:25,310 --> 00:03:34,000
separated by commas. We've got our right
square bracket on the right hand side.

45
00:03:34,000 --> 00:03:39,880
Line three, chosen underscore exit equals
and two double quotes, meaning an empty

46
00:03:39,890 --> 00:03:47,810
string. Now our while loop is going to be
while chosen exit not in available exits

47
00:03:47,810 --> 00:03:53,890
colon, and indented on the next line,
chosen underscore exit is equal to input

48
00:03:53,890 --> 00:03:58,940
parentheses double quotes
Please choose a direction

49
00:03:59,980 --> 00:04:03,880
colon space then we've got our double
quote and right parenthesis. Then on line

50
00:04:03,890 --> 00:04:10,609
seven print and double quotes in
parentheses, aren't you glad you

51
00:04:10,609 --> 00:04:18,829
got out of there. So in this particular room
in the game, the available exits are in

52
00:04:18,829 --> 00:04:24,110
the available underscore exits list;
north, south, east and west. In order to

53
00:04:24,110 --> 00:04:28,220
enter the loop at all, we have to
initialize chosen underscore exit to a

54
00:04:28,220 --> 00:04:32,390
value that isn't in the list. We could
have used anything except one of

55
00:04:32,390 --> 00:04:36,380
those directions, but an empty string
makes sense here. Because an empty string

56
00:04:36,380 --> 00:04:40,970
is not in the list, the condition
evaluates to True and the program enters

57
00:04:40,970 --> 00:04:45,200
the loop. And it's a very simple loop -
it just requests input from the player,

58
00:04:45,200 --> 00:04:49,580
and assignes what they type to the
chosen underscore exit variable. The

59
00:04:49,580 --> 00:04:53,570
loop then tests the condition again. If
the direction they typed is not one of

60
00:04:53,570 --> 00:04:57,530
the ones in the list, then the code block
and the loop is executed again. This

61
00:04:57,530 --> 00:05:01,880
continues until they type one of the valid
directions and escape. So if you run the

62
00:05:01,880 --> 00:05:07,040
program now, and we'll enter various
strings when prompted to see what

63
00:05:07,040 --> 00:05:11,960
happens. I'm gonna start by entering up.
Now that's not in the list so we get the

64
00:05:11,960 --> 00:05:18,240
input prompt again, down, also not in the
list. Then we go around the loop again.

65
00:05:18,240 --> 00:05:27,080
Then when I enter east here, the condition
evaluates to True and we leave the loop

66
00:05:27,080 --> 00:05:30,620
One of the important features of while
loops is that they can be used when you

67
00:05:30,639 --> 00:05:34,990
can't determine, in advance, how many
times you will need to loop. A for loop

68
00:05:34,990 --> 00:05:38,680
will repeat for each item in a
predetermined sequence, whereas with a

69
00:05:38,680 --> 00:05:42,480
while loop you don't need to know how
many times the loop will execute.

70
00:05:42,480 --> 00:05:45,900
One particular application of this is
reading data from a file or an internet

71
00:05:45,910 --> 00:05:50,650
stream. It's difficult, not impossible but
difficult, to know in advance how much

72
00:05:50,650 --> 00:05:54,880
data there is to read. A while loop can be
used to keep reading until there's no

73
00:05:54,880 --> 00:05:59,020
more data left. And we'll see this soon
when we look at file I/O, input and

74
00:05:59,020 --> 00:06:02,139
output, in Python.
In the next video, we'll look at break

75
00:06:02,139 --> 00:06:07,650
and continue with while loops.
See you in the next video.

