WEBVTT 1 00:00:00.480 --> 00:00:02.230 Hi guys, and welcome back. 2 00:00:02.230 --> 00:00:06.330 In this video we're going to learn about while loops. 3 00:00:06.330 --> 00:00:09.160 Loops in programming are used to repeat things 4 00:00:09.160 --> 00:00:10.950 a number of times. 5 00:00:10.950 --> 00:00:13.030 And there are two types of loops in Python. 6 00:00:13.030 --> 00:00:15.920 We've got while loops, and we've got for loops. 7 00:00:15.920 --> 00:00:18.193 In this video we'll learn about while loops. 8 00:00:19.620 --> 00:00:22.290 Let's start with a simple boolean. 9 00:00:22.290 --> 00:00:24.770 We will have a variable called is_learning, 10 00:00:24.770 --> 00:00:27.260 and we will make it equal to true. 11 00:00:27.260 --> 00:00:30.160 So at the moment we are learning, 12 00:00:30.160 --> 00:00:32.793 so we can type while is_learning, 13 00:00:34.950 --> 00:00:36.610 print "You're learning!" 14 00:00:36.610 --> 00:00:39.350 So this is our first while loop. 15 00:00:39.350 --> 00:00:42.780 What we've got here is the while key word, 16 00:00:42.780 --> 00:00:44.900 and then very much like an if statement, 17 00:00:44.900 --> 00:00:48.030 we have a boolean, and this can be a variable 18 00:00:48.030 --> 00:00:50.010 such as is_learning, which is true, 19 00:00:50.010 --> 00:00:53.940 it can be a comparison, it can be a number or anything else, 20 00:00:53.940 --> 00:00:58.130 then we have the colon which, just like in the if statement, 21 00:00:58.130 --> 00:01:02.190 signals the start of the body, and the body 22 00:01:02.190 --> 00:01:05.570 comes underneath that and is always indented with 23 00:01:05.570 --> 00:01:10.570 usually four spaces, and then we have the print statement. 24 00:01:10.610 --> 00:01:12.610 Again, just like the if statement, you can put 25 00:01:12.610 --> 00:01:15.590 multiple things in the body as long as they're all indented, 26 00:01:15.590 --> 00:01:19.380 and you can un-indent a line in order to signal 27 00:01:19.380 --> 00:01:20.973 the end of the while loop. 28 00:01:22.130 --> 00:01:24.080 I must say at this point that if you just leave it 29 00:01:24.080 --> 00:01:25.870 like this, that's totally fine, 30 00:01:25.870 --> 00:01:27.660 Python knows that you've reached the end of the file 31 00:01:27.660 --> 00:01:29.860 so that also terminates the while loop, 32 00:01:29.860 --> 00:01:32.170 but if you do want to add something else, you can do, 33 00:01:32.170 --> 00:01:33.593 as long as it's un-indented. 34 00:01:34.460 --> 00:01:36.900 Now what will happen if we run this while loop 35 00:01:36.900 --> 00:01:38.753 is something maybe unexpected. 36 00:01:40.430 --> 00:01:43.740 The while loop repeats this code in the body 37 00:01:43.740 --> 00:01:47.403 for as long as this condition is true. 38 00:01:48.290 --> 00:01:51.820 So for how long will this condition be true? 39 00:01:51.820 --> 00:01:55.100 Well, here's the thing, this condition never changes. 40 00:01:55.100 --> 00:01:57.630 It's always true because we don't set it 41 00:01:57.630 --> 00:02:00.200 to anything else anywhere else. 42 00:02:00.200 --> 00:02:02.380 So if we run this, you will see 43 00:02:02.380 --> 00:02:04.660 that we're learning a lot, and we maybe 44 00:02:04.660 --> 00:02:06.470 don't want to do that, we do want to take a break 45 00:02:06.470 --> 00:02:08.830 every now and then, and you see that we've printed 46 00:02:08.830 --> 00:02:11.310 a lot of these "You're learning!" 47 00:02:11.310 --> 00:02:15.300 Which is good, but what if we only wanted to run this 48 00:02:15.300 --> 00:02:17.200 a certain amount of times? 49 00:02:17.200 --> 00:02:20.923 Say, until the user told us that they're no longer learning? 50 00:02:21.950 --> 00:02:24.380 Well, in order to do that, in order to stop 51 00:02:24.380 --> 00:02:26.780 this loop from running at some point, 52 00:02:26.780 --> 00:02:29.830 we have to set is_learning to false. 53 00:02:29.830 --> 00:02:33.540 So if we do that like this, is_learning equals false, 54 00:02:33.540 --> 00:02:34.880 here's what will happen. 55 00:02:34.880 --> 00:02:37.200 We will start the loop, is_learning is true, 56 00:02:38.080 --> 00:02:40.740 so we will run it at least once. 57 00:02:40.740 --> 00:02:42.770 We will print that "You're learning!" 58 00:02:42.770 --> 00:02:45.300 and then we will set is_learning to false. 59 00:02:45.300 --> 00:02:50.240 Then we go back to the top, and we will evaluate this again. 60 00:02:50.240 --> 00:02:52.350 Now we will check if it's true again, 61 00:02:52.350 --> 00:02:56.450 but it's not, it's false, so it will stop, 62 00:02:56.450 --> 00:03:00.220 and it will continue from after the while loop onwards. 63 00:03:00.220 --> 00:03:03.460 If it were true, it would just repeat over and over 64 00:03:03.460 --> 00:03:06.410 as you've seen, but because we set it to false 65 00:03:06.410 --> 00:03:09.853 the first time that we run it, it only runs once. 66 00:03:11.130 --> 00:03:11.963 Just like that. 67 00:03:13.040 --> 00:03:15.960 So what we do is instead we will ask the user 68 00:03:15.960 --> 00:03:18.477 whether they are learning, we will say input, 69 00:03:18.477 --> 00:03:20.170 "Are you still learning?" 70 00:03:20.170 --> 00:03:21.253 And we will run this. 71 00:03:22.410 --> 00:03:24.880 So now we'll see that "You're learning!" at least once, 72 00:03:24.880 --> 00:03:28.030 because is_learning is true initially, and then 73 00:03:28.030 --> 00:03:31.020 we ask the user whether they are still learning. 74 00:03:31.020 --> 00:03:34.770 Now, what do we have to type here in order 75 00:03:34.770 --> 00:03:36.373 to continue the loop? 76 00:03:37.500 --> 00:03:41.470 Well, anything will go, such as yes. 77 00:03:41.470 --> 00:03:45.520 That is because now is_learning is no longer true, 78 00:03:45.520 --> 00:03:50.520 now is_learning is the string "yes" that we've typed. 79 00:03:50.820 --> 00:03:53.630 And if you remember from our if statements video, 80 00:03:53.630 --> 00:03:56.700 this gets passed through the bool function, 81 00:03:56.700 --> 00:03:59.270 and it evaluates to true when it's yes. 82 00:03:59.270 --> 00:04:04.230 The boolean of yes is true, but if we type an empty string, 83 00:04:04.230 --> 00:04:08.023 then the loop ends because we have terminated the loop. 84 00:04:09.590 --> 00:04:12.780 So maybe this is not what we want, maybe what we want 85 00:04:12.780 --> 00:04:17.780 is to get some user input, and then say that is_learning 86 00:04:17.870 --> 00:04:22.480 is equal to user_input == "yes". 87 00:04:22.480 --> 00:04:25.660 And what happens here, is that first of all we store 88 00:04:25.660 --> 00:04:28.700 the user's input into its own variable, 89 00:04:28.700 --> 00:04:32.810 and then we compare it with the string "yes". 90 00:04:32.810 --> 00:04:35.720 If they are the same, then this will be true, 91 00:04:35.720 --> 00:04:39.010 and if they are not the same, then it will be false, 92 00:04:39.010 --> 00:04:41.690 and we put that into is_learning. 93 00:04:41.690 --> 00:04:46.650 So now, only if we type "yes" will the loop continue, 94 00:04:46.650 --> 00:04:50.173 otherwise, if we type "no" for example, the loop will end. 95 00:04:53.930 --> 00:04:58.110 This here is a very common way of doing user menus, 96 00:04:58.110 --> 00:05:00.530 or running applications where users can repeat 97 00:05:00.530 --> 00:05:02.093 something multiple times. 98 00:05:03.420 --> 00:05:05.990 Here is an example of one such menu. 99 00:05:05.990 --> 00:05:07.690 The first thing we do in this programme is 100 00:05:07.690 --> 00:05:09.810 we get the user's input and we ask them 101 00:05:09.810 --> 00:05:12.260 if they wish to run the programme. 102 00:05:12.260 --> 00:05:14.990 Then, we have our while loop which checks 103 00:05:14.990 --> 00:05:18.687 that the user_input variable is equal to "yes". 104 00:05:19.780 --> 00:05:21.750 If it is, we will start the loop, 105 00:05:21.750 --> 00:05:23.750 and we print that we're running. 106 00:05:23.750 --> 00:05:26.040 Then we ask for the user_input again, 107 00:05:26.040 --> 00:05:27.253 exactly the same thing. 108 00:05:28.410 --> 00:05:31.100 And after that, we will go back to the start 109 00:05:31.100 --> 00:05:33.480 of the while loop, and we will compare user_input, 110 00:05:33.480 --> 00:05:36.700 which now has the new value, against "yes". 111 00:05:36.700 --> 00:05:38.910 If they are still the same, then we run again, 112 00:05:38.910 --> 00:05:41.580 and we ask again, and so on. 113 00:05:41.580 --> 00:05:43.510 This is how you can repeat a piece of code 114 00:05:43.510 --> 00:05:46.700 many times, until a user tells you to stop. 115 00:05:46.700 --> 00:05:49.690 So when would you want to use a while loop? 116 00:05:49.690 --> 00:05:51.270 When you want to repeat something 117 00:05:51.270 --> 00:05:53.910 an undefined number of times. 118 00:05:53.910 --> 00:05:56.360 For example, until a user tells you to stop. 119 00:05:56.360 --> 00:05:59.000 You don't know how many times you're going to repeat it. 120 00:05:59.000 --> 00:06:01.120 You depend on the user's input. 121 00:06:01.120 --> 00:06:03.890 The for loop, on the other hand, will be used to repeat 122 00:06:03.890 --> 00:06:06.850 something a defined number of times. 123 00:06:06.850 --> 00:06:09.190 Let's learn about that in the next video. 124 00:06:09.190 --> 00:06:10.140 I'll see you there!