课程: Python Essential Training

While

- While loops look innocuous, but they're one of the most dangerous things you can write in Python. One small mistake and your program will run forever. Granted, some programs we want to run forever, like a web service, but your program might run forever and not in a good way. But don't be afraid, while you're here, you'll learn advanced while loop control statements to tame these wild beasts. Let's go to the code. One thing I like to use to test while loops is the Python datetime class, from datetime import datetime. All right and sure, we could increment some numbers in our while loop to check if we've reached a particular number yet, but why wouldn't we just use a for loop for that? Checking the actual time is much more exciting and we can get the current date and time by calling datetime.now() and if we just want the seconds in the current minute, we can use .second so we're 34 seconds into the current minute. Now, if we want to get two seconds into the future, just add 2, but you have to be careful with this. Because let's say we're currently at 59 seconds, it's at one second into the next minute, so we need to use modulus 60 to reflect that. So, here's why we're doing all of this, let's make a simple while loop that waits two seconds and then prints out some message, wait_until equals two seconds into the future while datetime.now().second != wait_until: print ('Still waiting.') Finally, we exit our while loop and print "We are at {wait_until} seconds!" Okay, now if we run this, it very quickly fills up our screen. So Python is very fast, let me clear this cell. All right, so what do we do in this situation? Well, if we remove the print statement, we get a syntax error. You have to have something indented under the while loop. What we could do is maybe do some useless calculation. This works but it's an awful waste of computing power in my opinion. One thing we can use though is the pass statement. (instructor typing) There we go and what this past statement says is, nothing to see here move along but it does critically preserve our indenting. It's also a great thing to use as a placeholder say, if you're writing a function or a class definition, but you don't want to fill it in yet, you can just write pass. So let's take a look at another way to write this using the break statement. So I'm going to use wait_until, and then I'm going to say while True: all right, so honestly, my heart skips a beat whenever I see while True: in someone's code but in this case it's safe, we're going to use it safely. If datetime.now().second = wait_until: break now let me move this under the break statement there. All right, so it waits two seconds and then prints. So what the break statement does is break out of the current loop that it's in. You can think of this as going up the lines and saying, is this a loop? Nope, it's an if statement. Is this a loop? Yep, it's a while loop so it exits the current while loop, even though it says while True: Note, that the break statement only breaks out of the first loop that it's in. So if I do this and change it to a while loop as well, it waits two seconds, prints we are at 11 seconds... Whoops, and it's still running. You can see the asterisk there that indicates that it's still running. So it will run for ever, it never breaks out of this while True: And every time the seconds reach 11 seconds, it'll just print this message for a whole second. So let me stop this and I'm going to clear the cell output again. Okay, and the final control statement for, for loops, Continue. Continue, skip any lines that follow it inside of the while loop. So if I take this original code up here, then bring it down there... Just going to put a continue statement there and you can see that this line never prints. So continue just skips everything else that follows it inside this while loop. Now obviously, we could replace this entire thing with pass and it would be exactly the same and a little bit cleaner. So usually you see a continue statement used inside an if statement to prevent the rest of the code in the loop from being executed but only if some condition is met. So let's just copy this, change the while to True: again, And then in here I'm going to say if datetime.now().second is less than wait_until: continue change this to break. Okay, so when this continue statement is hit, it skips everything else in the loop, which is just this break statement. And as soon as this, if statement is no longer true, then continue is no longer hit, it actually reaches the break statement, breaking it outside of this while loop. Then it prints "We are at {wait_until} seconds!" Of course, this logic is basically the same as putting this check inside the while loop itself and then exiting the while loop after two seconds. And this is the big secret with continue and break, they're not technically logically necessary but they can help you rearrange and write code more clearly for other programmers to read. So it's important to understand them and be ready to whip them out to depending on the situation.

内容