WEBVTT 1 00:00:01.900 --> 00:00:06.520 so Python provides two different ways to loop around the block of code with for 2 00:00:06.520 --> 00:00:10.799 loops and while now we’ve seen how a for can be used to iterate through 3 00:00:10.799 --> 00:00:15.419 an iterable so either the items in a sequence or the numbers in a range but 4 00:00:15.419 --> 00:00:18.970 sometimes we actually need to keep looping as long as some condition 5 00:00:18.970 --> 00:00:24.570 is true and then stop when it becomes false, now we do this using a while loop, so 6 00:00:24.570 --> 00:00:30.240 the basic format of while loop is just a while and some condition colon and execute a block 7 00:00:30.240 --> 00:00:35.110 of code let's just look at an example of a for loop we have done earlier in this section 8 00:00:35.110 --> 00:00:47.140 and in contrast that to while loop, so start off by typing 9 00:00:47.140 --> 00:00:56.860 and we can run that, again we’ve seen that code in this section 10 00:00:56.860 --> 00:01:04.699 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:04.699 --> 00:01:11.770 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:11.770 --> 00:01:22.850 and we do something like 13 00:01:22.850 --> 00:01:33.210 same thing same placement field format, and so if we run that 14 00:01:33.210 --> 00:01:35.100 to confirm thats the same 15 00:01:35.100 --> 00:01:39.960 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:39.960 --> 00:01:43.690 iterate through a range of values like we’ve done here but this does give you a 17 00:01:43.690 --> 00:01:47.720 good example to see what's actually going so the first thing to note here is 18 00:01:47.720 --> 00:01:50.890 the condition on line 4 i < 10 19 00:01:50.890 --> 00:01:55.530 well that controls execution of the block of code in the loop which is the code 20 00:01:55.530 --> 00:02:00.619 on lines five and six so in other words as long as i is less than 10 21 00:02:00.619 --> 00:02:05.610 then code on lines 5 and 6 is going to be executed but once it becomes equal to or greater than 22 00:02:05.610 --> 00:02:11.139 10 the condition becomes false and the loops terminates, in other words there no more processing and you saw that 23 00:02:11.139 --> 00:02:13.830 because when i was less than 10, is now 9 and 24 00:02:13.830 --> 00:02:18.560 we automatically skipped that point and that was the end but these leads to two other important points to 25 00:02:18.560 --> 00:02:24.220 consider here, one first the condition must be initialized before the loop starts of 26 00:02:24.220 --> 00:02:29.150 you saw me do that on line 3 when initialised i to the value of 0, now with 27 00:02:29.150 --> 00:02:34.120 the for loop of course we just set i without setting the value first and that's because python knows that 28 00:02:34.120 --> 00:02:38.180 to give it the first value from the range and to continue from there but with a 29 00:02:38.180 --> 00:02:43.530 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:43.530 --> 00:02:48.150 distinction that you have to do when using while loops but the 31 00:02:48.150 --> 00:02:52.300 second thing that is important here is we we need some way within the loop for the 32 00:02:52.300 --> 00:02:57.100 condition to become false so just to show you what I mean if we remove this, we remove the 33 00:02:57.100 --> 00:03:00.930 indent, put them back to the left hand side so as its no longer indented, and we run that 34 00:03:00.930 --> 00:03:06.050 you can see we got a loop happening on the right hand side the code is being executed and 35 00:03:06.050 --> 00:03:11.520 I'll just stop that you could see there was no way for the condition to become 36 00:03:11.520 --> 00:03:16.640 false in other words there was no way to exit because i was never been incremented 37 00:03:16.640 --> 00:03:21.220 so this condition would forever stay true, will 38 00:03:21.220 --> 00:03:25.250 always have i less than 10 in that situation and you saw an example here to 39 00:03:25.250 --> 00:03:28.709 the right that I was always 0 so consequently we can never have a false 40 00:03:28.709 --> 00:03:33.360 condition so the loop’s kept looping infinitely and just out of interest if 41 00:03:33.360 --> 00:03:36.890 you want to do that we decided we wanted to leave infinitely we could have to 42 00:03:36.890 --> 00:03:41.820 done while true and run it, we will achieve the same results you can see 43 00:03:41.820 --> 00:03:48.459 there, i’ll stopped that now, now there are some scenarios that you'll see loops that just use 44 00:03:48.459 --> 00:03:52.519 true as a condition but the body of the loop then contains a way to break out of 45 00:03:52.519 --> 00:04:01.190 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:01.190 --> 00:04:05.580 for loop is preferable in this particular and it will be very rare to see a while loop 47 00:04:05.580 --> 00:04:10.440 use like that because frankly there's more work more code effectively got four 48 00:04:10.440 --> 00:04:14.489 lines of code they're doing what two lines of the for loop of the lines 1 and 2, but there 49 00:04:14.489 --> 00:04:18.200 are many reasons for looping around the block of code that don't involve just 50 00:04:18.200 --> 00:04:22.140 going through items in a sequence which we did in this case so for example in an 51 00:04:22.140 --> 00:04:24.700 adventure game players to have choose a direction 52 00:04:24.700 --> 00:04:28.330 in order to exit from a location, now if they don't choose one of the available exits 53 00:04:28.330 --> 00:04:34.780 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:34.780 --> 00:04:50.920 this code out and say something like 55 00:04:50.920 --> 00:05:11.520 and will just initialise and empty string and then we can put 56 00:05:11.520 --> 00:05:35.470 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:35.470 --> 00:05:41.320 something like north please choose a direction because it wasn't in the available 58 00:05:41.320 --> 00:05:48.300 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:48.300 --> 00:05:52.920 things about this is we initialize the variable chosen_exit on line 9 60 00:05:52.920 --> 00:05:58.740 to set to an empty string and so that meant the first time we started the while loop chosen_exit 61 00:05:58.740 --> 00:06:02.770 wasn't in the available exits and therefore was able to go through and asked 62 00:06:02.770 --> 00:06:10.190 what the direction it was, if we had initialized that variable to east lets say and run this you see 63 00:06:10.190 --> 00:06:13.780 that the loop didn’t get process at all because the first condition while 64 00:06:13.780 --> 00:06:18.890 chosen_exits not in available exits that returned false in other words chosen 65 00:06:18.890 --> 00:06:22.900 exit was unavailable exit so consequently this line's code was skipped 66 00:06:22.900 --> 00:06:28.730 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:28.730 --> 00:06:32.010 looping effectively until we’ve got a valid 68 00:06:32.010 --> 00:06:37.639 input, or the exit that we’ve chosen is in the list that we define on line 7 69 00:06:37.639 --> 00:06:41.340 so probably the most important feature certainly one of the most important 70 00:06:41.340 --> 00:06:45.379 features of while loops is that it can be used in this scenario and that's where 71 00:06:45.379 --> 00:06:49.909 you can’t determine in advance how many times you actually need to loop so for 72 00:06:49.909 --> 00:06:54.080 loop will repeat for each item in a predetermined sequence whereas as you 73 00:06:54.080 --> 00:06:57.969 see here in a while loop you don't need to know ahead of time many times the legal 74 00:06:57.969 --> 00:07:02.120 execute because of course in this scenario this could continue sort of 75 00:07:02.120 --> 00:07:06.849 hundreds of times until the player typed in a direction that was on our list on line 76 00:07:06.849 --> 00:07:12.050 7, so one particular application of this would be reading data from a file now it's 77 00:07:12.050 --> 00:07:15.860 difficult not impossible but certainly difficult to know in advance how much 78 00:07:15.860 --> 00:07:20.080 data is there read from a file in your computer now the while loop can be used to keep 79 00:07:20.080 --> 00:07:24.639 repeating until there's no more data left now will see this soon when we look at file input 80 00:07:24.639 --> 00:07:28.139 and output which is in a future section on this course let's just enhance our 81 00:07:28.139 --> 00:07:32.229 example a little bit because just like with the for loop we can also use break 82 00:07:32.229 --> 00:07:36.960 and continue to break out of the loop or to cause the loop to stop the current iteration 83 00:07:36.960 --> 00:07:40.419 and to test the condition again, so breaks can provide a simple way 84 00:07:40.419 --> 00:07:43.550 to quit in other words without choosing a valid exit so what we could to there is 85 00:07:43.550 --> 00:07:59.120 we could do something like, now if we run that 86 00:07:59.120 --> 00:08:11.599 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:11.599 --> 00:08:15.800 was printed out and then we exited the game with a break, so the break exited out 88 00:08:15.800 --> 00:08:20.729 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:20.729 --> 00:08:27.080 valid one, east, that still works, but we probably shouldn't print the final 90 00:08:27.080 --> 00:08:30.969 message if the player chooses quit , so just like a for loop we can also use 91 00:08:30.969 --> 00:08:36.240 else, the one you saw in the for loop section, so otherwise down here we can put 92 00:08:36.240 --> 00:08:45.440 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:45.440 --> 00:08:56.529 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:56.529 --> 00:09:00.640 over and you can see that because of the else the statement wasn't printed and we can just put 95 00:09:00.640 --> 00:09:05.600 a valid one like east, we should find it does actually show that 96 00:09:05.600 --> 00:09:10.860 message on the screen, now on the early lecture on if statements if you recall 97 00:09:10.860 --> 00:09:14.779 we created a very simple guessing game that's what the player had to guess a number 98 00:09:14.779 --> 00:09:18.120 between 1 and 10, know there are a number of problems with that game one 99 00:09:18.120 --> 00:09:21.250 problem was that it only allowed two guesses if he wanted to allow more than 100 00:09:21.250 --> 00:09:25.980 two guesses but we have to do, would involved with be the nesting of more and more 101 00:09:25.980 --> 00:09:30.610 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:30.610 --> 00:09:35.339 a bit boring not that its terribly engaging to begin with but the 103 00:09:35.339 --> 00:09:38.990 first problem we talked about their can be solved using a while loop in the other words 104 00:09:38.990 --> 00:09:42.060 if we want to allow more than two guesses and you'll be doing that in the challenge 105 00:09:42.060 --> 00:09:46.980 next, but before we do that let's take digress slowly and address the second problem to 106 00:09:46.980 --> 00:09:50.470 produce a more interesting game that's because resolving the situation where 107 00:09:50.470 --> 00:09:56.319 the correct number to guess was always 5 so to fix the problem the regarding correct number being 5 108 00:09:56.319 --> 00:09:59.420 what we need to do is get the computer to generate a random number in the chosen 109 00:09:59.420 --> 00:10:04.899 range, from one to ten in the example of this game but of course we can extend that 110 00:10:04.899 --> 00:10:10.730 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:10.730 --> 00:10:13.680 of course more than that, and you’ve seen a lot of that already as we go through the 112 00:10:13.680 --> 00:10:17.370 course, and lists, iterators, etc you're going to be dealing 113 00:10:17.370 --> 00:10:20.399 with in the next section so there's enough built in fact for us to be able 114 00:10:20.399 --> 00:10:25.480 to write a random number generator if you want to do this but great news is that 115 00:10:25.480 --> 00:10:30.149 one has already been written for us and ships with python in a module called 116 00:10:30.149 --> 00:10:34.089 Python so we're gonna be looking at creating modules and packages much later 117 00:10:34.089 --> 00:10:38.470 in the course but for now what we’re gonna do is used to one that comes with it, and because 118 00:10:38.470 --> 00:10:42.430 it's an external module we need to import it into our program using the import 119 00:10:42.430 --> 00:10:46.740 statement, now don't worry too much about this now all you 120 00:10:46.740 --> 00:10:50.930 really need to know to use it is as we have to import it which tells Python 121 00:10:50.930 --> 00:10:54.910 which location to grab that particular piece of code so that we can 122 00:10:54.910 --> 00:10:57.320 actually take advantage of that and use it in our 123 00:10:57.320 --> 00:11:02.630 game let's now go ahead and type in that game, I’m going to comment out this code again 124 00:11:02.630 --> 00:11:11.820 like so, so instead of typing lets go ahead and i’ll just paste in here first and 125 00:11:11.820 --> 00:11:14.820 we'll go through it because it's essentially exactly the same as the 126 00:11:14.820 --> 00:11:19.640 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:19.640 --> 00:11:24.440 got import random, thats the command which tells Python we want to take 128 00:11:24.440 --> 00:11:28.730 advantage of this pre written code to generate random numbers and you can see 129 00:11:28.730 --> 00:11:34.340 on line 21 , first thing we started with the highest to find a variable called 130 00:11:34.340 --> 00:11:38.990 highest which is set to a value of 1 and on line 21 we got the answer which is a random 131 00:11:38.990 --> 00:11:43.200 number between one and the highest number which in this case 10 some in other 132 00:11:43.200 --> 00:11:48.200 words answer should be a number between one and 10 each time and you can see 133 00:11:48.200 --> 00:11:52.830 we the modified program to also display, put the numbers, please guess 134 00:11:52.830 --> 00:11:56.420 number between one and we can put the number and whatever however we wish 135 00:11:56.420 --> 00:12:00.210 at the moment its set to highest and that means you could change this number if you wanted to 136 00:12:00.210 --> 00:12:03.490 to make that sort of twenty fifty a hundred or whatever you want to do 137 00:12:03.490 --> 00:12:07.210 and then we got the code which is essentially the same as what it was before going to the guess scenario 138 00:12:07.210 --> 00:12:12.350 but of course it's still a little bit limited in that its only going to give two chances to try 139 00:12:12.350 --> 00:12:16.480 it and probably in most cases you need at least 4 so lets try running this to 140 00:12:16.480 --> 00:12:21.330 confirm that it works, please guess a number between 1 and 10, we can try 5 141 00:12:21.330 --> 00:12:29.830 first please guess higher, 8, Sorry you have not guessed correctly, try again and of course it didn't help us 142 00:12:29.830 --> 00:12:34.490 because it didn’t tell us what the number was either try again and see what we can get lucky, five 143 00:12:34.490 --> 00:12:43.430 again, please guess higher, try 8, sorry you have not guess correctly, will try one more time, 5 lower 144 00:12:43.430 --> 00:12:44.980 this time, will try 3 145 00:12:44.980 --> 00:12:50.330 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:50.330 --> 00:12:53.350 had two times it said the number is higher the second time was lower 147 00:12:53.350 --> 00:12:58.510 so clearly its generating a random number each time, but of course the game method exists is very 148 00:12:58.510 --> 00:13:02.380 frustrating and the reason that it's frustrating of course is that we only got 149 00:13:02.380 --> 00:13:05.520 two chances so what we probably need is a minute 150 00:13:05.520 --> 00:13:09.950 three or maybe four guesses to be sure of getting that number correct so that's going to give 151 00:13:09.950 --> 00:13:13.460 us a good challenge to try and so what we'll do is we'll look at that challenge 152 00:13:13.460 --> 00:13:14.460 in the next video