-
Notifications
You must be signed in to change notification settings - Fork 0
/
typetrainer.py
52 lines (38 loc) · 1.29 KB
/
typetrainer.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
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
"""A simple typetrainer that shows a random string and checks if it matches
the user input"""
import random
import string
class WordGenerator:
"""Generator for random words consting of both uppercase, lowercase,
digits, and special characters.
"""
def __init__(self, charlist=False, word_length=5):
self.charlist = charlist if charlist else string.printable[:94]
self.n_chars = len(self.charlist)
self.word_length = word_length
def __next__(self):
"""Generate a random word with the specified length (default=5)
"""
return ''.join(
[
self.charlist[random.randint(0, self.n_chars - 1)]
for _
in range(0, self.word_length)
]
)
def main():
"""Show a word, check if user input matches, repeat
"""
print('Type the characters (excluding "> " and hit enter. To quit, enter "exit" as answer.')
word_generator = WordGenerator()
while True:
word = next(word_generator)
print(word)
answer = input("> ")
if answer in ['quit', 'exit']:
quit()
if answer != word:
print('You typed "{}" instead of "{}"'.format(answer, word))
if __name__ == "__main__":
main()