-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
69 lines (63 loc) · 2.34 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import random
import time
class Setup:
def __init__(self):
print("WELCOME TO HARD TYPING TRAINING")
self.words = list()
self.practice_time = 0
self.used_index = list()
self.count = 0
with open('words.txt') as file:
for line in file:
if len(line) != 1 or len(line) < 10:
self.count += 1
self.words.append(line.replace("\n",""))
def get_time(self):
self.practice_time = input("How long do you have to Practice (mins)?\n")
try:
self.practice_time = 60*int(self.practice_time)
print("Practice time set to: {} seconds".format(self.practice_time))
except ValueError:
print("Enter a valid number in minutes")
self.get_time()
def generate_rand_words(self):
rand_words = list()
while len(rand_words) < 8:
index = random.randint(0, self.count)
if index not in self.used_index:
self.used_index.append(index)
rand_words.append(self.words[index])
return rand_words
def grade_words(self, words_list, user_string):
if isinstance(user_string, str):
total_words = len(words_list)
user_in_list = user_string.split()
user_in_list = user_in_list[:total_words]
corrects = 0
for i in range(len(user_in_list)):
if words_list[i] == user_in_list[i]:
corrects +=1
return corrects
else:
print("Invalid input by user")
return 0
def load_application(self):
if not self.practice_time:
self.get_time()
start_ts = time.time()
corrects = 0
total_words = 0
while time.time() - start_ts < self.practice_time:
words_list = master.generate_rand_words()
print(" ".join(words_list))
user_in = input()
corrects += master.grade_words(words_list, user_in)
total_words += len(words_list)
print("Your Accuracy is {:.2f} ".format(corrects*100/total_words))
retry = input("Do you want to start again?(y/n)")
if retry == "y":
self.load_application()
else:
print("Bye, see you again !")
master = Setup()
master.load_application()