Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
zmx0142857
GitHub Repository: zmx0142857/mini-games
Path: blob/master/py/arithmetic.py
363 views
1
from random import randint, choice
2
from time import time
3
from sys import argv
4
5
correct = 0
6
mistake = 0
7
mistakes = []
8
begin = time()
9
beginq = begin
10
if len(argv) < 3:
11
print('usage: python arithmetic.py min max [count]\nexample: python arithmetic.py 12 15')
12
exit(0)
13
a = int(argv[1])
14
b = int(argv[2])
15
count = int(argv[3]) if len(argv) >= 4 else 15
16
speed = {}
17
18
def stat():
19
total = correct + mistake
20
print("\rcorrect: %d/%d" % (correct, total))
21
if total > 0:
22
speed_value = 60*correct/(time()-begin)
23
print(f"speed: {speed_value:.2f}")
24
print('\n'.join(f' {v[0]}: {v[1]:.2f}' for v in sorted([(k, 60*speed[k][1]/speed[k][0]) for k in speed], key=(lambda t:t[1]), reverse=True)))
25
if mistakes:
26
print("mistakes: %s" % ', '.join('%d*%d' % elem for elem in set(mistakes)))
27
28
def get_operands(a, b):
29
if len(mistakes) > 0 and randint(0,6) == 0:
30
return choice(mistakes)
31
else:
32
return randint(a,b), randint(2,9)
33
34
for cnt in range(count):
35
i, j = get_operands(a, b)
36
ans = i * j
37
while True:
38
try:
39
user = int(input("%d x %d = " % (i, j)))
40
except EOFError:
41
stat()
42
exit()
43
except ValueError:
44
print("invalid input")
45
continue
46
if user == ans:
47
correct += 1
48
now = time()
49
if i in speed:
50
speed[i] = (speed[i][0]+now-beginq, speed[i][1]+1)
51
else:
52
speed[i] = (now-beginq, 1)
53
beginq = now
54
else:
55
mistake += 1
56
print("answer: %d" % ans)
57
mistakes.append((i,j))
58
break
59
stat()
60
61