How to Insert a Delay in Python Code
The Python "time.sleep" function lets you pause your Python script for a specified amount of time. You use this function when you want to give the user a specific amount of time to send input or pause the program while other processes finish. You must specified an amount of time in seconds that you want the program to pause. After the time is reached, the program execution continues.
Instructions
-
-
1
Open your Python editor and open the Python source code file in which you want to set the pause timer.
-
2
Add the "time" library. You must have this library included to use the "timer" class. Copy and paste the following code to the top of your code file:
import time
-
-
3
Click the section of code where you want the program to pause. The timer counts down and stops code execution, so place the pause function in a location where it does not interfere with user interaction.
-
4
Add the following code to pause code execution:
time.sleep(5)
In this example, the code pauses for five seconds. Use any integer value or decimal value for fractional seconds.
-
1