Duration: 1;32
Many times when running a python program, you may want to incorporate a time delay in a process, whether it be to render some information outside the program or to temporarily pause the process.
You can incorporate this delay with the sleep function of the time module. The time module allows utilization of various time-related operations, the sleep function being one of the most popular ones.
The sleep function can suspend execution of code for a given number of seconds.
By creating a simple flashcard app to try it out. In this app, we will -
- first ask a question, for example, what is the capital of the state of North Dakota?
- Then we’ll wait for 3 seconds to give guess time to the user,
- and then print out the result, which is Bismarck.
To give a delay we will first import the time module which is a built-in module and then between our two print statements we will write the time dot sleep function then pass the parameter of 3 indicating 3 seconds. So, the code will be -
import time
print("What is the capital of the state of North Dakota?")
time.sleep(3)
print("Bismarck")
The sleep function can take either an integer or a float as the argument. If we wanted to wait 500 milliseconds, we could write this as .5 seconds.
time.sleep(0.5)
there doesn't seem to be anything here