4.1 Use a "while" loop - Video Tutorials & Practice Problems
Video duration:
9m
Play a video:
Video transcript
<v ->Okay, so now let's start looking at some while loops.</v> So while loops are like something that will go around and around, and keep repeating until you tell it to stop. And there are a few different ways of telling a while loop to stop, and so let's look at some of them. Okay, so let's open up scratch.py and you can delete anything that was there before. And we'll start a while loop with the word while. That is easy to remember. And then we're gonna have some sort of condition in here. So it could just be while true, and then do this thing. Print. Okay, and so I can run this, and it's just going to keep printing this, and I can stop the program and the file here, by pressing the stop button. This is not very good. We wanna probably have it stop at some point, but there are some cases where you just want to keep something running until the user shuts it down. So let's look at how we can stop this. So one way we could do that is having a variable. So, like, x equals zero. And then we can print x here. And one thing we can do is say, like, once x reaches 20, while x is less than or equal to 20, print x, and x equals x plus one. So we're gonna start at zero, increase x by one every time, and then stop when this condition returns false. So let's run this, and we can see it prints from zero to 20. So that's one way to get out of while loops. We can have a condition in here. If it's true, it'll print everything in this block, so all the stuff that's indented to the same amount. And then if it's false, it'll stop and go out here. Print, stopped looping. So I can run that again and it'll print this at the very end. Okay, so another way of breaking out of a loop is if we have a break statement. So we can have that here. If x is equal to eight, break. So let's see what happens now. So now, it stopped at seven, so it didn't print that seven, and that's because inside of here, x equals seven, x does not equal eight, print seven, and then add one to it. So now x equals eight, and then it goes here, that's true. This is true, it goes to break and it never gets to this print statement. So that's one of the tricky things about loops, is if you are basing the loop off of some value, like when are you changing that value? When does the while loop stop and when are you breaking out of it? So there are some kind of tricky things in here, so one thing you could do is just change it by putting it up here, and if we do that and check for x equals eight afterwards, then now eight gets printed. So the order of stuff that happens here matters. Yeah, then those are basically two of the ways of breaking out of while loops. One thing we can also do is have a continue keyword, and that just means skip everything that happens afterwards in the while loop, but keep looping. Don't break out of it completely. So we can say if x mod two is equal to one, continue. So what this does is, remember modulo is saying the remainder of x, divided by this number is what the modulo gives you. So this is checking if it's odd, then continue. Keep, like, skip all this stuff. Go back to this loop. If it's even, then print and then if x equals eight, then break. And if we remove this, we can, like, it won't break, and it'll just stop looping when this condition is false. We'll work now on problem number seven. And we wanna have a New Year's countdown. So open up problem seven, New Year's, and we wanna start at 10 seconds, count down until one, and then print Happy New Year. So we've got the print Happy New Year going, and we'll just run this. That's good. So if we wanna start at 10 seconds, we can say something like seconds left equals 10, and then we're gonna print a while loop. So we could just say print 10, print nine, whatever, and keep going, but of course, that's not the most efficient way of writing this code. So let's use a while loop. While seconds left is greater than zero, then print seconds left. So let's see what happens when I do this. And it just keeps printing 10, and that's because I'd forgotten to decrement seconds left every time. So seconds left equals seconds left minus one. And if you remember from earlier, we can actually rewrite this as seconds_left minus equals one. So now let's run this, and got 10 down to one. It's great. If we had had an equals there, it would go to zero, and then Happy New Year, but we actually don't want that zero to come up. We want it to just go to one. Okay, great. And then lastly, we want to actually pause in between each of these, and pause for one second. So you can look this up. This is a very Google-able thing. Python how do pause for one second. And I'll just show you, you can import time, and then at the end of this loop, we can say time dot sleep and it takes in the number of seconds, and we'll sleep for one second. So ten, nine, and that looks good. Great. So now that you have this, it's easy to change any of these values. Like, we can sleep for just 0.1 seconds so that it goes faster. But we can start at, say, 20 seconds left if we wanted to. And we don't have to rewrite a whole lot of code. We just need to change this one number and it'll loop according to that number. Yeah, so just looking at these examples as an overview of how to do while loops, the different ways of breaking are while this evaluates to true, do everything inside of it. When it's false, go out of it. Or, if a condition is met, then break out of it.