-
Notifications
You must be signed in to change notification settings - Fork 0
/
timed math challenge.py
50 lines (37 loc) · 1.26 KB
/
timed math challenge.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
import random
import time
# values ----------------------------------
OPERATORS = ["*", "-", "+"]
MIN_OPERAND = 3
MAX_OPERAND = 12
NO_OF_QUESTIONS = 1
# operations ------------------------------
def problem_generator():
left = random.randint(MIN_OPERAND, MAX_OPERAND)
right = random.randint(MIN_OPERAND, MAX_OPERAND)
operator = random.choice(OPERATORS)
expression = f"{str(left)} {operator} {str(right)}"
result = eval(expression)
return expression, result
input("press Enter to start")
print("------------------------")
start_time = time.time()
def get_value():
for i in range(NO_OF_QUESTIONS):
expression, result = problem_generator()
while True:
try:
answer = int(input(f"Problem #{i + 1}: {expression} = "))
if answer == result:
break
elif answer != result:
print("Incorrect")
else:
print("Invalid input")
except ValueError:
pass
end_time = time.time()
total_time = end_time - start_time
print("------------------------")
print(f"Well done! you did it in {total_time:.2f} seconds!")
get_value()