forked from jcandan/math_quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_quiz.py
executable file
·89 lines (74 loc) · 3.73 KB
/
math_quiz.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
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/python3
import time
import random
import operator
import argparse
from argparse import RawTextHelpFormatter
def main():
questions = []
operators = {}
time_0 = time.time()
description = (
"Description:\n"
"\n"
"Runs a timed quiz of math equations based on a range from 1 to 10."
"User can specify the start and end of the range for both terms.\n"
)
epilog = (
"Examples:\n"
"\n"
"python3 math_quiz.py \n"
"python3 math_quiz.py -r Randomize entire set of terms\n"
"python3 math_quiz.py -as Add and Subtract entire set of terms\n"
"python3 math_quiz.py -asr Add and Subtract randomized entire set of terms\n"
"python3 math_quiz.py -s1 5 -e1 5 Add 5's\n"
"python3 math_quiz.py -s1 5 -e1 5 -as Add and Subtract 5's\n"
"python3 math_quiz.py -s1 5 -e1 5 -e2 5 -md Multipy and Divide 5 with 1 - 5\n"
)
parser = argparse.ArgumentParser(prog = 'python3 math_quiz.py', usage = '%(prog)s [options]',
description = description, epilog = epilog, formatter_class = RawTextHelpFormatter)
parser.add_argument("-r", "--random", help = "Randomize set of questions.", action = "store_true")
parser.add_argument("-s1", "--start_first", help = "Specify start of range for first term",
type = int, default = 1)
parser.add_argument("-e1", "--end_first", help = "Specify end of range for first term",
type = int, default = 10)
parser.add_argument("-s2", "--start_second", help = "Specify start of range for second term",
type = int, default = 1)
parser.add_argument("-e2", "--end_second", help = "Specify end of range for second term",
type = int, default = 10)
parser.add_argument("-a", "--add", help = "(Default) Add the terms", action = "store_true")
parser.add_argument("-s", "--subtract", help = "Subtract the terms", action = "store_true")
parser.add_argument("-m", "--multiply", help = "Multiply the terms", action = "store_true")
parser.add_argument("-d", "--divide", help = "Divide the terms", action = "store_true")
args = parser.parse_args()
if args.add or not (args.subtract or args.multiply or args.divide):
operators['add'] = '+'
if args.subtract:
operators['sub'] = '-'
if args.multiply:
operators['mul'] = 'x'
if args.divide:
operators['truediv'] = '/'
terms_1 = list(reversed(range(args.start_first, args.end_first + 1)))
terms_2 = list(reversed(range(args.start_second, args.end_second + 1)))
for term_1 in terms_1:
for term_2 in terms_2:
for method, symbol in operators.items():
questions.append({'first': term_1, 'second': term_2, 'method': method, 'symbol': symbol})
if args.random:
random.shuffle(questions)
for question in questions:
# evaluate an expression for the given terms and operation, e.g. operator.add(3, 4)
expression = "operator.{}({}, {})".format(question['method'], question['first'], question['second'])
correct = eval(expression)
# prompt user with a neatly formatted math question
question_str = "{} {} {} = ".format(str(question['first']), question['symbol'], str(question['second']))
user_response = input(question_str)
if str(correct) != user_response:
# note the user's incorrect response
print("Incorrect :( . . . The answer is " + str(correct))
time_delta = time.time() - time_0
m, s = divmod(time_delta, 60)
print('Time: %02d:%02d' % (m, s))
if __name__ == "__main__":
main()