Typing speed test with accuracy using python
import time
def calculate_accuracy(reference_text, typed_text):
reference_length = len(reference_text)
typed_length = len(typed_text)
# Ensure the typed text doesn't exceed the reference length
if typed_length > reference_length:
typed_text = typed_text[:reference_length]
correct_chars = sum(1 for ref, typed in zip(reference_text, typed_text) if ref == typed)
accuracy = (correct_chars / reference_length) * 100
return accuracy
def typing_speed_test(reference_text):
print("Type the following text without deleting characters:")
print(reference_text)
input("Press Enter when you're ready...")
print("Start typing:")
user_input = ""
start_time = time.time()
while len(user_input) < len(reference_text):
char = input()
user_input += char # Add user input to the typed text
print(reference_text[:len(user_input)]) # Display the typed part of the reference text
end_time = time.time()
elapsed_time = end_time - start_time
accuracy = calculate_accuracy(reference_text, user_input)
words_in_reference = len(reference_text.split())
words_per_minute = (words_in_reference / elapsed_time) * 60
print(f"Time elapsed: {elapsed_time:.2f} seconds")
print(f"Typing speed: {words_per_minute:.2f} words per minute")
print(f"Accuracy: {accuracy:.2f}%")
if __name__ == "__main__":
reference_text = "The quick brown fox jumps over the lazy dog."
typing_speed_test(reference_text)
Comments
Post a Comment