This depends on your code logic. But from the code supplied, most probably it is Timer from threading module, so you just have to add this at the top of your code
from threading import Timer
Documentation is here: threading.Timer
Answer from Ammar Hasan on Stack OverflowVideos
This depends on your code logic. But from the code supplied, most probably it is Timer from threading module, so you just have to add this at the top of your code
from threading import Timer
Documentation is here: threading.Timer
Even though this is probably not the case here given the context, it is also possible that the Timer is from the timeit module which allows to run tests on the quickness of execution.
That is, from timeit import Timer
https://docs.python.org/3/library/timeit.html
» pip install timer-for-python
The time module
The time module allows the user to directly get the time, in seconds, since 1970 (See: https://docs.python.org/3/library/time.html). This means that we can subtract the time before from time after to see how long it has been, namely how long it took the user to finish the typing test. From there, it is as easy as printing the result. You can round the time using int to get a purely seconds result without milliseconds.
The code
# Import the time library
import time
# Calculate the start time
start = time.time()
# Code here
# Calculate the end time and time taken
end = time.time()
length = end - start
# Show the results : this can be altered however you like
print("It took", length, "seconds!")
You can use the built-in time library:
import time
str_to_type = "The cat is catching a mouse."
start_time = time.perf_counter()
print("Type: '" + str_to_type + "'.")
typedstring = input()
if typedstring == str_to_type:
end_time = time.perf_counter()
run_time = end_time - start_time
print("You typed '" + str_to_type + "' in", str(run_time), "seconds.")
I have a computer science gcse project and part of it is creating a timer alongside a test to time the user on how long they take to complete it which that data is then transferred into a dedicated file. I’ve gotten no further then creating the time module, does anyone have any advice on how to start the timer/ stopwatch and then check how long a task took to do? I don’t mind I don’t mind whether it’s in min or sec.