Typing Speed Test in Python.
About the project: The program will display a short paragraph of text for you to type.
It will then calculate your typing speed in words per minute (WPM) and the accuracy of your typing.
The app will use Python's time module to time your typing session.
This Python script is a complete and interactive typing speed test. When you run it, it will provide a passage for you to type and then calculate your WPM and accuracy.
The program will continue to run until you choose to quit.
Project Level: Beginner
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# typing_speed_test.py
import time
import random
def get_text_to_type():
"""
Returns a random passage of text for the user to type.
You can easily add more passages to the list.
"""
passages = [
"The quick brown fox jumps over the lazy dog.",
"Python is a high-level, interpreted programming language.",
"Typing fast and accurately is a valuable skill in the digital age.",
"To be a programmer, one must know how to code in multiple languages.",
"Practice makes perfect when it comes to improving your typing speed."
]
return random.choice(passages)
def calculate_wpm(start_time, end_time, typed_text):
"""
Calculates the words per minute (WPM).
Formula: WPM = (number of words / time) * 60
"""
time_taken = end_time - start_time
# Assuming words are separated by spaces
words = typed_text.split()
num_words = len(words)
wpm = (num_words / time_taken) * 60
return wpm
def calculate_accuracy(original_text, typed_text):
"""
Calculates the accuracy percentage.
"""
correct_chars = 0
min_len = min(len(original_text), len(typed_text))
for i in range(min_len):
if original_text[i] == typed_text[i]:
correct_chars += 1
# Accuracy is based on the number of correct characters typed vs. the original text length
accuracy = (correct_chars / len(original_text)) * 100
return accuracy
def main():
"""
Main function to run the Typing Speed Test.
"""
print("--- Python Typing Speed Test ---")
while True:
text_to_type = get_text_to_type()
print("\nGet ready to type. Type the following passage:")
print("-------------------------------------------------")
print(text_to_type)
print("-------------------------------------------------")
# Wait for the user to press Enter to start the timer
input("Press Enter when you are ready to start...")
# Start the timer
start_time = time.time()
# Get user's typed input
typed_text = input("Start typing here: ")
# End the timer
end_time = time.time()
# Calculate WPM and accuracy
wpm = calculate_wpm(start_time, end_time, typed_text)
accuracy = calculate_accuracy(text_to_type, typed_text)
print("\n--- Results ---")
print(f"Words Per Minute (WPM): {wpm:.2f}")
print(f"Accuracy: {accuracy:.2f}%")
print("---------------")
play_again = input("\nDo you want to take another test? (yes/no): ").strip().lower()
if play_again != 'yes':
print("Exiting Typing Speed Test. Goodbye!")
break
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
main()