-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_math_challenge.py
78 lines (61 loc) · 2.05 KB
/
time_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
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
# Ask a question, don't allow to pass if answer incorrect, calculate how long
# it takes to answer it
import random as ra # for operands
import time
# Constants
OPERATORS = ["+", "-", "*"]
MIN_OPERAND = 3
MAX_OPERAND = 12
TOTAL_PROBLEMS = 10
def generate_problem() -> tuple:
"""Generates a random expression for the user
Returns:
tuple: contains the random expression with its answer.
"""
left = ra.randint(MIN_OPERAND, MAX_OPERAND)
right = ra.randint(MIN_OPERAND, MAX_OPERAND)
operator = ra.choice(OPERATORS)
expression = str(left) + " " + operator + " " + str(right)
# print(expression) # num operator num => 4 - 9
answer = eval(expression) # Evaluates as Python expression, using a string
return expression, answer
def test() -> int:
"""Executes the test
Returns:
int: total score of user.
"""
score: int = 0
for n in range(TOTAL_PROBLEMS):
expr, result = generate_problem()
while True:
answer = input(f"Problem #{n + 1}\n{expr} = ")
# if answer is converted to int but wrong input is given the program
# will crash. Since input is already a string compared with the result
# as a string, by not been the same it will just keep asking the right
# answer
if answer == str(result):
score += 1
break
else:
continue
return score
def start_program(t_problems: int) -> None:
"""Initialize the program
Args:
t_problems (int): Total number of problems which conforms the test
Returns:
NoneType: None
"""
print("-" * 80)
start_test: int = int(time.time())
score: int = test()
finish_test: int = int(time.time())
print("-" * 80)
total_time: int = finish_test - start_test
print(f"You finish the test in {total_time} seconds\n"
f"You're final score is:", (score * 100 / t_problems))
def main() -> None:
input("Press enter to start")
start_program(TOTAL_PROBLEMS)
if __name__ == "__main__":
main()