1
00:00:05,300 --> 00:00:09,920
so Python provides two different ways to
loop around the block of code with for

2
00:00:09,920 --> 00:00:14,190
loops and while now we’ve seen how a for can be used to iterate through

3
00:00:14,190 --> 00:00:18,810
an iterable so either the items in a
sequence or the numbers in a range but

4
00:00:18,810 --> 00:00:22,370
sometimes we actually need to
keep looping as long as some condition

5
00:00:22,370 --> 00:00:27,970
is true and then stop when it becomes
false, now we do this using a while loop, so

6
00:00:27,970 --> 00:00:33,640
the basic format of while loop is just a while and
some condition colon and execute a block

7
00:00:33,640 --> 00:00:38,510
of code let's just look at an example of
a for loop we have done earlier in this section

8
00:00:38,510 --> 00:00:50,540
and in contrast that to while loop, so
start off by typing

9
00:00:50,540 --> 00:01:00,260
and we can run that, again we’ve seen that code in this section

10
00:01:00,260 --> 00:01:08,090
nothing new there, i is now 0 through to 9 as you can see, because we use the range 10 so that's a for loop now with a

11
00:01:08,090 --> 00:01:15,170
while loop we can produce the same results so what what we can do is i’ll just comment that code out

12
00:01:15,170 --> 00:01:26,250
and we do something like

13
00:01:26,250 --> 00:01:36,610
same thing same placement field format, and so if we run that

14
00:01:36,610 --> 00:01:38,500
to confirm thats the same

15
00:01:38,500 --> 00:01:43,360
we get exactly the same result that we got in the for loop, you probably wouldn't use a while loop just to

16
00:01:43,360 --> 00:01:47,090
iterate through a range of values like
we’ve done here but this does give you a

17
00:01:47,090 --> 00:01:51,120
good example to see what's actually
going so the first thing to note here is

18
00:01:51,120 --> 00:01:54,290
the condition on line 4 i < 10

19
00:01:54,290 --> 00:01:58,930
well that controls execution of the block of code
in the loop which is the code

20
00:01:58,930 --> 00:02:04,010
on lines five and six so in
other words as long as i is less than 10

21
00:02:04,010 --> 00:02:09,009
then code on lines 5 and 6 is going to be executed but once it becomes equal to or greater than

22
00:02:09,009 --> 00:02:14,530
10 the condition becomes false and the loops terminates, in other words there no more processing and you saw that

23
00:02:14,530 --> 00:02:17,230
because when i was less than 10, is now 9 and

24
00:02:17,230 --> 00:02:21,960
we automatically skipped that point and that was the end but these leads to two other important points to

25
00:02:21,960 --> 00:02:27,620
consider here, one first the condition
must be initialized before the loop starts of

26
00:02:27,620 --> 00:02:32,550
you saw me do that on line 3 when initialised i to the value of 0, now with

27
00:02:32,550 --> 00:02:37,520
the for loop of course we just set i without setting the value first and that's because python knows that

28
00:02:37,520 --> 00:02:41,580
to give it the first value from the
range and to continue from there but with a

29
00:02:41,580 --> 00:02:46,930
while loop we had to give i a value first if we didn't get an error that is not defined so thats another

30
00:02:46,930 --> 00:02:51,550
distinction that you have to do when
using while loops but the

31
00:02:51,550 --> 00:02:55,700
second thing that is important here is we we need
some way within the loop for the

32
00:02:55,700 --> 00:03:00,500
condition to become false so just to show you what I mean if we remove this, we remove the

33
00:03:00,500 --> 00:03:04,330
indent, put them back to the left
hand side so as its no longer indented, and we run that

34
00:03:04,330 --> 00:03:09,450
you can see we got a loop happening on the right hand
side the code is being executed and

35
00:03:09,450 --> 00:03:14,920
I'll just stop that you could see there
was no way for the condition to become

36
00:03:14,920 --> 00:03:20,040
false in other words there was no way
to exit because i was never been incremented

37
00:03:20,040 --> 00:03:24,620
so this condition would forever stay true, will

38
00:03:24,620 --> 00:03:28,650
always have i less than 10 in that
situation and you saw an example here to

39
00:03:28,650 --> 00:03:32,100
the right that I was always 0 so consequently we can never have a false

40
00:03:32,100 --> 00:03:36,760
condition so the loop’s kept looping
infinitely and just out of interest if

41
00:03:36,760 --> 00:03:40,290
you want to do that we decided we wanted
to leave infinitely we could have to

42
00:03:40,290 --> 00:03:45,220
done while true and run it, we will
achieve the same results you can see

43
00:03:45,220 --> 00:03:51,850
there, i’ll stopped that now, now there are some
scenarios that you'll see loops that just use

44
00:03:51,850 --> 00:03:55,910
true as a condition but the body of the
loop then contains a way to break out of

45
00:03:55,910 --> 00:04:04,590
the loop, you can see in this scenario, and i’ll just undo the working example we had,  so you can see that why

46
00:04:04,590 --> 00:04:08,980
for loop is preferable in this particular and it will be very rare to see a while loop

47
00:04:08,980 --> 00:04:13,840
use like that because frankly there's
more work more code effectively got four

48
00:04:13,840 --> 00:04:17,880
lines of code they're doing what two lines of
the for loop of the lines 1 and 2, but there

49
00:04:17,880 --> 00:04:21,600
are many reasons for looping around the
block of code that don't involve just

50
00:04:21,600 --> 00:04:25,540
going through items in a sequence which we did in this case so for example in an

51
00:04:25,540 --> 00:04:28,100
adventure game players to have choose a
direction

52
00:04:28,100 --> 00:04:31,730
in order to exit from a location, now if they don't choose one of the available exits

53
00:04:31,730 --> 00:04:38,180
they stay where they are and thats an ideal use of a while loop let's do
something that like, so will just comment

54
00:04:38,180 --> 00:04:54,320
this code out and say something like

55
00:04:54,320 --> 00:05:14,920
and will just initialise and empty string and then we can put

56
00:05:14,920 --> 00:05:38,870
and on the next line we put, so just to confirm that lets run this, please choose a direction we can put

57
00:05:38,870 --> 00:05:44,720
something like north please choose a direction
because it wasn't in the available

58
00:05:44,720 --> 00:05:51,700
options you see a on line 7 we got 3 options and we
typed something like south aren’t you glad you got out of there so the interesting

59
00:05:51,700 --> 00:05:56,320
things about this is we initialize the
variable chosen_exit on line 9

60
00:05:56,320 --> 00:06:02,140
to set to an empty string and so that meant the
first time we started the while loop chosen_exit

61
00:06:02,140 --> 00:06:06,170
wasn't in the available exits and
therefore was able to go through and asked

62
00:06:06,170 --> 00:06:13,590
what the direction it was, if we had initialized that
variable to east lets say and run this you see

63
00:06:13,590 --> 00:06:17,180
that the loop didn’t get process at all because the first condition while

64
00:06:17,180 --> 00:06:22,290
chosen_exits not in available exits that
returned false in other words chosen

65
00:06:22,290 --> 00:06:26,300
exit was unavailable exit so
consequently this line's code was skipped

66
00:06:26,300 --> 00:06:32,130
altogether we just got this print out there so we initialised it on
line 9 and then it's going to keep

67
00:06:32,130 --> 00:06:35,410
looping effectively until we’ve got a
valid

68
00:06:35,410 --> 00:06:41,030
input, or the exit that we’ve chosen is in the list
that we define on line 7

69
00:06:41,030 --> 00:06:44,740
so probably the most important feature
certainly one of the most important

70
00:06:44,740 --> 00:06:48,770
features of while loops is that it can be
used in this scenario and that's where

71
00:06:48,770 --> 00:06:53,300
you can’t determine in advance how many
times you actually need to loop so for

72
00:06:53,300 --> 00:06:57,480
loop will repeat for each item in a
predetermined sequence whereas as you

73
00:06:57,480 --> 00:07:01,360
see here in a while loop you don't need to
know ahead of time many times the legal

74
00:07:01,360 --> 00:07:05,520
execute because of course in this
scenario this could continue sort of

75
00:07:05,520 --> 00:07:10,240
hundreds of times until the player typed in
a direction that was on our list on line

76
00:07:10,240 --> 00:07:15,450
7, so one particular application of this would
be reading data from a file now it's

77
00:07:15,450 --> 00:07:19,260
difficult not impossible but certainly
difficult to know in advance how much

78
00:07:19,260 --> 00:07:23,480
data is there read from a file in your computer
now the while loop can be used to keep

79
00:07:23,480 --> 00:07:28,030
repeating until there's no more data left
now will see this soon when we look at file input

80
00:07:28,030 --> 00:07:31,530
and output which is in a future section
on this course let's just enhance our

81
00:07:31,530 --> 00:07:35,620
example a little bit because just like
with the for loop we can also use break

82
00:07:35,620 --> 00:07:40,360
and continue to break out of the loop or to
cause the loop to stop the current iteration

83
00:07:40,360 --> 00:07:43,810
and to test the condition again, so breaks can provide a simple way

84
00:07:43,810 --> 00:07:46,950
to quit in other words without choosing
a valid exit so what we could to there is

85
00:07:46,950 --> 00:08:02,520
we could do something like, now if we run that

86
00:08:02,520 --> 00:08:14,990
we still got the scenario if we enter something wrong like west, that’s not going to work but if we typed quit you can see game over

87
00:08:14,990 --> 00:08:19,200
was printed out and then we exited the
game with a break, so the break exited out

88
00:08:19,200 --> 00:08:24,120
of the while loop in that scenario and again if we just run again to confirm that it still works with a

89
00:08:24,120 --> 00:08:30,480
valid one, east, that still works, but
we probably shouldn't print the final

90
00:08:30,480 --> 00:08:34,360
message if the player chooses quit , so just like a for loop we can also use

91
00:08:34,360 --> 00:08:39,640
else, the one you saw in the for loop
section, so otherwise down here we can put

92
00:08:39,640 --> 00:08:48,840
else, and this is going to be executed just like the for loop, if the code hasn’t been broken out in other words

93
00:08:48,840 --> 00:08:59,920
a break hasn't been
processed so if we run, again will type quit, oops I put a space there, quit, we’ve now got game

94
00:08:59,920 --> 00:09:04,040
over and you can see that because of the else
the statement wasn't printed and we can just put

95
00:09:04,040 --> 00:09:09,000
a valid one like east, we should find it does actually show that

96
00:09:09,000 --> 00:09:14,260
message on the screen, now on the early lecture on if statements if you recall

97
00:09:14,260 --> 00:09:18,170
we created a very simple guessing game
that's what the player had to guess a number

98
00:09:18,170 --> 00:09:21,520
between 1 and 10, know there are a
number of problems with that game one

99
00:09:21,520 --> 00:09:24,650
problem was that it only allowed two
guesses if he wanted to allow more than

100
00:09:24,650 --> 00:09:29,380
two guesses but we have to do, would
involved with be the nesting of more and more

101
00:09:29,380 --> 00:09:34,010
if test, every time they guess it correctly was employed and the other problem was 5 was always the correct number which make the game

102
00:09:34,010 --> 00:09:38,730
a bit boring not that its terribly engaging to begin with but the

103
00:09:38,730 --> 00:09:42,390
first problem we talked about their can
be solved using a while loop in the other words

104
00:09:42,390 --> 00:09:45,460
if we want to allow more than two guesses and
you'll be doing that in the challenge

105
00:09:45,460 --> 00:09:50,380
next, but before we do that let's take digress
slowly and address the second problem to

106
00:09:50,380 --> 00:09:53,870
produce a more interesting game that's because resolving the situation where

107
00:09:53,870 --> 00:09:59,710
the correct number to guess was always 5 so to fix the problem the regarding correct number being 5

108
00:09:59,710 --> 00:10:02,820
what we need to do is get the computer to
generate a random number in the chosen

109
00:10:02,820 --> 00:10:08,290
range, from one to ten in the example
of this game but of course we can extend that

110
00:10:08,290 --> 00:10:14,130
now Python has got a lot of features built in such as the ability to do
arithmetic, lists, ranges, and tuples and

111
00:10:14,130 --> 00:10:17,080
of course more than that, and you’ve seen a lot of
that already as we go through the

112
00:10:17,080 --> 00:10:20,770
course, and lists, iterators, etc you're going to be dealing

113
00:10:20,770 --> 00:10:23,790
with in the next section so there's
enough built in fact for us to be able

114
00:10:23,790 --> 00:10:28,880
to write a random number generator if
you want to do this but great news is that

115
00:10:28,880 --> 00:10:33,540
one has already been written for us and
ships with python in a module called

116
00:10:33,540 --> 00:10:37,480
Python so we're gonna be looking at
creating modules and packages much later

117
00:10:37,480 --> 00:10:41,870
in the course but for now what we’re gonna do is
used to one that comes with it, and because

118
00:10:41,870 --> 00:10:45,830
it's an external module we need to import
it into our program using the import

119
00:10:45,830 --> 00:10:50,140
statement, now don't worry too much about this now all you

120
00:10:50,140 --> 00:10:54,330
really need to know to use it is as we
have to import it which tells Python

121
00:10:54,330 --> 00:10:58,310
which location to grab that particular piece of code so that we can

122
00:10:58,310 --> 00:11:00,720
actually take advantage of that and use
it in our

123
00:11:00,720 --> 00:11:06,030
game let's now go ahead and type in that
game, I’m going to comment out this code again

124
00:11:06,030 --> 00:11:15,220
like so, so instead of typing lets go ahead and
i’ll just paste in here first and

125
00:11:15,220 --> 00:11:18,220
we'll go through it because it's
essentially exactly the same as the

126
00:11:18,220 --> 00:11:23,040
previous game with a few extra lines of code so you can see
what I've done there so we started off

127
00:11:23,040 --> 00:11:27,840
got import random, thats the
command which tells Python we want to take

128
00:11:27,840 --> 00:11:32,130
advantage of this pre written code to
generate random numbers and you can see

129
00:11:32,130 --> 00:11:37,740
on line 21 , first thing we started with the
highest to find a variable called

130
00:11:37,740 --> 00:11:42,390
highest which is set to a value of 1 and on line 21 we got the answer which is a random

131
00:11:42,390 --> 00:11:46,600
number between one and the highest
number which in this case 10 some in other

132
00:11:46,600 --> 00:11:51,600
words answer should be a number between
one and 10 each time and you can see

133
00:11:51,600 --> 00:11:56,230
we the modified program to also
display, put the numbers, please guess

134
00:11:56,230 --> 00:11:59,820
number between one and we can put
the number and whatever however we wish

135
00:11:59,820 --> 00:12:03,610
at the moment its set to highest and that means you
could change this number if you wanted to

136
00:12:03,610 --> 00:12:06,890
to make that sort of twenty fifty a
hundred or whatever you want to do

137
00:12:06,890 --> 00:12:10,610
and then we got the code which is essentially the
same as what it was before going to the guess scenario

138
00:12:10,610 --> 00:12:15,750
but of course it's still a little bit limited
in that its only going to give two chances to try

139
00:12:15,750 --> 00:12:19,880
it and probably in most cases you need at least 4 so lets try running this to

140
00:12:19,880 --> 00:12:24,730
confirm that it works, please guess a number between 1 and 10, we can try 5

141
00:12:24,730 --> 00:12:33,230
first please guess higher, 8, Sorry you have not guessed correctly, try again and of course it didn't help us

142
00:12:33,230 --> 00:12:37,890
because it didn’t tell us what the number was either
try again and see what we can get lucky, five

143
00:12:37,890 --> 00:12:46,830
again, please guess higher, try 8, sorry you have not guess correctly, will try one more time, 5 lower

144
00:12:46,830 --> 00:12:48,380
this time, will try 3

145
00:12:48,380 --> 00:12:53,730
sorry you have not guessed correctly, so you can see that the point is that
that number has now changed anyway we

146
00:12:53,730 --> 00:12:56,750
had two times it said the number is
higher the second time was lower

147
00:12:56,750 --> 00:13:01,910
so clearly its generating a random number each
time, but of course the game method exists is very

148
00:13:01,910 --> 00:13:05,780
frustrating and the reason that it's
frustrating of course is that we only got

149
00:13:05,780 --> 00:13:08,920
two chances so what we probably need is a
minute

150
00:13:08,920 --> 00:13:13,350
three or maybe four guesses to be sure of getting that number correct so that's going to give

151
00:13:13,350 --> 00:13:16,860
us a good challenge to try and so what
we'll do is we'll look at that challenge

152
00:13:16,860 --> 00:13:17,860
in the next video

