1

How do you create a timer in python? My project is a speed typing test and the timer is there to time the length it takes the user to type. The first task the user types is the alphabet, as fast as they can and then the second task is to type as quickly as possible again for a group of words in set in a random order

2 Answers2

1

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 = start - end

# Show the results : this can be altered however you like
print("It took", start-end, "seconds!")
Larry the Llama
  • 958
  • 3
  • 13
-1

You can use the build in time libary:

import time
strToType="The cat is catching a mouse."
start_time = time.perf_counter()
print("Type: '"+strToType+"'.")
typedstring=input()
if typedstring==strToType:
    end_time = time.perf_counter()     
    run_time = end_time - start_time
    print("You typed '"+strToType+"' in "+str(run_time)+" seconds.")
The_spider
  • 1,202
  • 1
  • 8
  • 18