Overview
This article explains how to delay processes in the Python programming language by using a module called time, and one of its methods sleep.
Step 1: Import the Time Module
In Python, there is a module called "time" that is used in this script.
# time module
import time
Step 2: Make a Function
It is best practice to create functions to store a set of instructions that can be executed efficiently. A function is made to make the timer:
def timer_countdown():
*Code*
- Remember to indent within the next line to include the code within the function. The function in this demonstration is named timer_countdown.
Step 3: Making Inputs
A timer works by taking inputs to specify how long it wants to run. The timer was first designed to take the following inputs in this order: days, hours, minutes, seconds
def timer_countdown():
# Ask for how many days the timer will run for
how_many_days = int(input("How many days is needed to run the timer: "))
while how_many_days <= 0:
print("Invalid response.")
how_many_days = int(input("How many days is needed to run the timer: "))
# Ask for how many hours the timer will run for
how_many_hours = int(input("How many hours is needed to run the timer: "))
while how_many_hours <= 0 or how_many_hours > 24:
print("Invalid response.")
how_many_hours = int(input("How many hours is needed to run the timer: "))
# Ask for how many minutes the timer will run for
how_many_minutes = int(input("How many minutes is needed to run the timer: "))
while how_many_minutes <= 0 or how_many_minutes > 60:
print("Invalid response.")
how_many_minutes = int(input("How many minutes is needed to run the timer: "))
# Ask for how many seconds the timer will run for
how_many_seconds = int(input("How many seconds is needed to run the timer: "))
while how_many_seconds <= 0 or how_many_seconds > 60:
print("Invalid response.")
hours_to_seconds = int(input("How many seconds is needed to run the timer: "))
The code above implements how the timer takes four inputs to ask how many days, hours, minutes and seconds the timer will run for. While loops are also implemented to not give any negative numbers for the variables or positive numbers that are not in the range of hours, minutes or seconds.
Step 4: Convert the Inputs to the same Unit of Time
Since four different inputs are taken for days, hours, minutes and seconds, all of these variables need to be converted into the same unit of time, therefore of these variables were converted to seconds shown below and then added together to give the total time the timer should run for:
# Convert days, hours and minutes all to seconds
days_to_seconds = how_many_days * 24 * 60 * 60
hours_to_seconds = how_many_hours * 60 * 60
minutes_to_seconds = how_many_minutes * 60
# Add the given times together in seconds
total_seconds = days_to_seconds + hours_to_seconds + minutes_to_seconds + how_many_seconds
total_time = "The timer will run for " + str(how_many_days) + " days, " + str(how_many_hours) + " hours, " + str(how_many_minutes) + " minutes, " + str(how_many_seconds) + " seconds"
print(total_time)
Step 5: Make the Timer and run for the Specified Time
Importing the time module gives us access to writing functions with time such as "time.sleep()". "time.sleep()" takes a numerical input within the parenthesis to delay some computer processes from running back to back. With time.sleep(), the total amount of seconds can be inputted for the timer to run for the inputs that were received for the days, hours, minutes and seconds. Since all the code is within the function, a return statement must be included at the end to return variables or statements to run the code.
countdown = time.sleep(total_seconds)
timer_finished = "The timer stopped!"
return countdown, print(timer_finished)
Step 6: Run the Function in the Script file
Outside of the function, write the name of the function to execute the code
timer_countdown()
The Entire Script
import time
# This script asks for how many days, hours, minutes, and seconds a timer needs to run for
def timer_countdown():
# Ask for how many days the timer will run for
how_many_days = int(input("How many days is needed to run the timer: "))
while how_many_days <= 0:
print("Invalid response.")
how_many_days = int(input("How many days is needed to run the timer: "))
# Ask for how many hours the timer will run for
how_many_hours = int(input("How many hours is needed to run the timer: "))
while how_many_hours <= 0 or how_many_hours > 24:
print("Invalid response.")
how_many_hours = int(input("How many hours is needed to run the timer: "))
# Ask for how many minutes the timer will run for
how_many_minutes = int(input("How many minutes is needed to run the timer: "))
while how_many_minutes <= 0 or how_many_minutes > 60:
print("Invalid response.")
how_many_minutes = int(input("How many minutes is needed to run the timer: "))
# Ask for how many seconds the timer will run for
how_many_seconds = int(input("How many seconds is needed to run the timer: "))
while how_many_seconds <= 0 or how_many_seconds > 60:
print("Invalid response.")
hours_to_seconds = int(input("How many seconds is needed to run the timer: "))
# Convert days, hours and minutes all to seconds
days_to_seconds = how_many_days * 24 * 60 * 60
hours_to_seconds = how_many_hours * 60 * 60
minutes_to_seconds = how_many_minutes * 60
# Add the given times together in seconds
total_seconds = days_to_seconds + hours_to_seconds + minutes_to_seconds + how_many_seconds
total_time = "The timer will run for " + str(how_many_days) + " days, " + str(how_many_hours) + " hours, " + str(how_many_minutes) + " minutes, " + str(how_many_seconds) + " seconds"
print(total_time)
countdown = time.sleep(total_seconds)
timer_finished = "The timer stopped!"
return countdown, print(timer_finished)
timer_countdown()
Source Code
https://github.com/AndrewDass1/PROTOTYPE_IDEAS_AND_SCRIPTS/tree/main/Ideas/Timer