-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·187 lines (149 loc) · 6.76 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
#imports
import argparse
from collections import namedtuple
import string
import random
import sys
from time import time, ctime
from urllib import request
from colorama import Fore, Back, Style
from readchar import readkey, key
# function to test if number is positive
def posInt(s: str) -> int:
try:
v = int(s)
except ValueError:
raise argparse.ArgumentTypeError(f'expected integer, got {s!r}')
if v <= 0:
raise argparse.ArgumentTypeError(f'expected positive integer, got {v}')
return v
# function to define the output letters
def outputLetter(output_list):
output_list = []
for x in range(97,123):
letter = chr(x)
output_list.append(letter)
return output_list
#main function
def main():
received_letters = [] #list of received letters
requested_letters = [] #list of requested letters
duration = [] #Duration of the minigame
hit_time = [] #time of corrected answers
miss_time = [] #time of incorrected answers
# Definition of arguments for the inputs
parser=argparse.ArgumentParser(
description = '''Definition of test mode ''')
parser.add_argument('-utm', '--use_time_mode', action='store_true',
help = " Mode: Time")
parser.add_argument('-mv', '--max_value',nargs='?', const=10, type=posInt, required=True,
help = " Input number.")
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
args = parser.parse_args()
# Initial print
print(Style.BRIGHT + Fore.RED + 'PARI '+Style.RESET_ALL+"Typing Test, group 5, October 2022")
# detect which mode is active and assosiate the arguments
if args.use_time_mode == True:
if str(args.max_value) == 'None':
print('ERROR: Insert a -mv (MAX_NUMBER)')
sys.exit(1)
else:
print('Test running up to '+Style.BRIGHT + Fore.GREEN + str(args.max_value) + ' seconds'+Style.RESET_ALL)
else:
print('Test running up to '+Style.BRIGHT + Fore.GREEN+ str(args.max_value) + ' inputs'+Style.RESET_ALL)
# Standby, waiting for the user to press any key
print(Style.BRIGHT + Fore.BLUE+'Press any key to start the test'+Style.RESET_ALL)
readkey()
# timmers and counters
start_time = time()
test_start_time = ctime()
number_characters = -1
hit_counter = 0
# condition for the time mode
if args.use_time_mode == True:
#Creation of while loop to control the break
while True:
end_time = time()
diferenca = end_time - start_time #counts time for each
if diferenca <= int(args.max_value):
# minigame concept
requested = random.choice(outputLetter(''))
print('Type letter '+ Fore.YELLOW + requested + Style.RESET_ALL)
received = readkey()
# break if user press spacebar
if received == chr(32):
test_end_time = ctime()
break
else:
# correct answer
if requested == received:
print('You typed ' + Fore.GREEN + received + Style.RESET_ALL)
hit_counter += 1
hit_end_time = time()
tempo_certo = hit_end_time-end_time
duration.append(round(tempo_certo,3))
hit_time.append(tempo_certo)
# incorrect answer
else:
print('You typed ' + Fore.RED + received + Style.RESET_ALL)
end_time_errado = time()
tempo_errado = end_time_errado-end_time
duration.append(round(tempo_errado,3))
miss_time.append(tempo_errado)
else:
# when you stop playing because of time
print(Style.BRIGHT + Fore.RED + str(round(diferenca,3)) + Style.RESET_ALL + ' segundos. Passou o limite de ' + Style.BRIGHT + Fore.GREEN + str(args.max_value) + Style.RESET_ALL + ' segundos.')
test_end_time = ctime()
break
received_letters.append(received)
requested_letters.append(requested)
# condition for the max value game
else:
#Creation of while loop to control the break
while True:
end_time = time()
diferenca = end_time - start_time
number_characters += 1 #counter of characters answered
if number_characters < int(args.max_value):
#minigame concept
requested = random.choice(outputLetter(''))
print('Type letter ' + Fore.YELLOW + requested + Style.RESET_ALL)
received = readkey()
# break if user press spacebar
if received == chr(32):
test_end_time = ctime()
break
else:
# correct answer
if requested == received:
print('You typed ' + Fore.GREEN + received + Style.RESET_ALL)
hit_counter += 1
hit_end_time = time()
tempo_certo = hit_end_time - end_time
duration.append(round(tempo_certo,3))
hit_time.append(tempo_certo)
# incorrect answer
else:
print('You typed ' + Fore.RED + received + Style.RESET_ALL)
end_time_errado = time()
tempo_errado = end_time_errado - end_time
duration.append(round(tempo_errado,3))
miss_time.append(tempo_errado)
else:
# when you stop playing because of time
print(Style.BRIGHT + Fore.RED + str(number_characters) + Style.RESET_ALL + ' characters. Reached the limit of ' + Style.BRIGHT + Fore.GREEN + str(args.max_value) + Style.RESET_ALL + ' characters.')
test_end_time = ctime()
break
received_letters.append(received)
requested_letters.append(requested)
#determine accuracy. if it had no right answers accuracy = 0
if len(requested_letters) == 0:
accuracy = 0
else:
accuracy = round(hit_counter/len(requested_letters),3)
return received_letters, requested_letters, hit_counter,diferenca ,test_start_time,test_end_time, accuracy, hit_time, miss_time, duration
if __name__ == '__main__':
main()