-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_math.py
More file actions
55 lines (50 loc) · 1.24 KB
/
cmd_math.py
File metadata and controls
55 lines (50 loc) · 1.24 KB
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
import random
from time import time
def get_ans(num1:int, num2:int, op:int) -> int:
'''Prompts user to enter the answer to a question.
Returns user inputted answer as int
'''
ans = None
while(ans is None):
try:
if(op == 0):
ans = int(input(f'What is {num1} * {num2}? ' ))
else:
dividend = num1 * num2
ans = int(input(f'What is {dividend} / {num1}? ' ))
except ValueError:
print("Please enter a number!")
return ans
def main():
total = 0
num_correct = 0
duration = 0
random.seed()
while(duration < 10):
correct = False
num1 = random.randint(1, 10)
num2 = random.randint(0, 10)
op = random.randint(0,1)
user_ans = get_ans(num1, num2, op)
total += 1
if total == 1:
start = time()
if(op == 0):
if(user_ans == num1 * num2):
num_correct += 1
correct = True
else:
if(user_ans == num2):
num_correct += 1
correct = True
if not correct:
print('Wrong!!!!!\n')
percent = num_correct * 100 / total
now = time()
duration = (now-start)/60
print(f'{duration:.2f} min: {num_correct}/{total}, {percent:.1f}%')
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass