-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath-test.py
More file actions
38 lines (29 loc) · 910 Bytes
/
Copy pathmath-test.py
File metadata and controls
38 lines (29 loc) · 910 Bytes
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
import random
import time
OPERATORS = ["+", "-", "*",]
MIN_OPERAND = 2
MAX_OPERAND = 12
TOTAL_PROBLEMS = 5
def generate_problem():
left = random.randint(MIN_OPERAND, MAX_OPERAND)
right = random.randint(MIN_OPERAND, MAX_OPERAND)
operator = random.choice(OPERATORS)
expr = f"{left} {operator} {right}"
answer = eval(expr)
return expr, answer
wrong = 0
input("Press Enter to start!")
print("----------------------")
start_time = time.time()
for _ in range(TOTAL_PROBLEMS):
expr, answer = generate_problem()
while True:
guess = input("Problem #" + str(_ + 1) + ": " + expr + " = ")
if guess == str(answer):
print("Correct!")
break
wrong += 1
end_time = time.time()
total_time = end_time - start_time
print("----------------------")
print(f"Nice work! You finished in {total_time:.2f} seconds with {wrong} wrong answers.")