In this video, learn how to use the time module to create a timer in Python. You can use the sleep method from the library to have your program count time for you.
- [Instructor] Let's create a timer with the Time module. Of course, to use the time module we have to import it. So we'll go import time. And the way the timer is going to work is it's going to go from zero to 10, and then once we hit 10 we're going to stop the timer and say we're done. And so we'll want to ask the user, when should we start the timer? We're going to have this variable called run, and we're going to use the input function that we used in a previous video, start, and then a little arrow here so the user can then say yes we want to start.
And so we'll have this other variable called seconds, which is going to keep track of how many seconds have passed. We'll start it at zero, and then we'll say if run, meaning what the user responded, equals yes, then we're going to go ahead and start the timer. The way the timer's going to work is we're going to say, while we haven't reached 10 seconds, then we're going to go ahead and continue running the timer. And so the first thing we'll want to do with this timer is print out how many seconds have passed, in this case it'll just be in the variable seconds.
So at first it's going to be zero seconds, but then later on we're going to increment it. And as the while loop continues, we'll get to 10, and then once we hit 10 we're going to break out of the while loop. So we can use the time module to see that a second has passed. So we'll go time.sleep, which will stop our program for one second, and then we'll go ahead and increment seconds here. And then seconds will be one, we'll go up here, we'll see that one does not equal 10, we'll go ahead and print out the one second, we'll sleep for another second, and then seconds will be two because it will be incremented again until we hit 10, and then eventually we will break out once we hit 10 and the program will end.
Running this, we see our start. If we say anything else but yes, nothing happens. But say we say yes, yes, we get this little timer here that goes zero, one, two, three, and it's going to go all the way to 10 and when it hits 10 we're just going to exit the program because there's nothing else that will run. The reason that this didn't go all the way to 10 is because we set our stopping point at 10 here. If we wanted it to go all the way to 10, we could add another line of code here, print, and then this line again, adding in those seconds.
And this would allow us to print 10. And so when we say yes here, it will print all the way to 10. Eight, nine, we hit 10, and then it exits. This can be useful for any applications that use some kind of timing or timer mechanism, as well as those where keeping track of time is important.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content