-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyperacer.py
43 lines (33 loc) · 1.21 KB
/
typeracer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import time
import random
sentences = [
"The quick brown fox jumps over the lazy dog.",
"Practice makes perfect.",
"Typing fast is a valuable skill.",
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.",
"In a world where you can be anything, be kind."
]
def typing_race():
# Select a random sentence
sentence = random.choice(sentences)
print("Type the following sentence as quickly as you can:")
print(f"\n{sentence}\n")
# Start timer
start_time = time.time()
# Take user input
user_input = input("Your attempt: ")
# End timer
end_time = time.time()
# Calculate typing speed
time_taken = end_time - start_time
words_typed = len(user_input.split())
words_per_minute = (words_typed / time_taken) * 60
# Calculate accuracy
correct_words = sum(1 for u, s in zip(user_input.split(), sentence.split()) if u == s)
accuracy = (correct_words / len(sentence.split())) * 100
# Display results
print("\nResults:")
print(f"Time taken: {time_taken:.2f} seconds")
print(f"Words per minute: {words_per_minute:.2f} WPM")
print(f"Accuracy: {accuracy:.2f}%")
typing_race()