-
Notifications
You must be signed in to change notification settings - Fork 4
/
arithmetic.py
96 lines (81 loc) · 2.84 KB
/
arithmetic.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
90
91
92
93
94
95
96
# standard libraries
import random
class Arithmetic(object):
"""
Used for Addition, Subtraction, Multiplication and Division Questions
"""
def __init__(self, min_num=0, max_num=10):
"""
:param min_num: Minimum number to start with.
:param max_num: Maximum number to start with.
"""
self.max_num = max_num
self.min_num = min_num
self.operation = None
self.num_one = None
self.num_two = None
self.QUESTION = "What is {} {} {}?"
def prep_rand_num(self):
"""
:return: None. Gives num_one and num_two random values from min and max.
"""
self.num_one = random.randint(self.min_num, self.max_num)
self.num_two = random.randint(self.min_num, self.max_num)
def get_addition_question(self):
"""
:return: A addition question.
"""
self.prep_rand_num()
self.operation = "+"
return self.QUESTION.format(self.num_one, self.operation, self.num_two)
def get_subtraction_question(self):
"""
:return: A subtraction question.
"""
self.prep_rand_num()
self.operation = "-"
while self.num_one < self.num_two:
self.prep_rand_num()
return self.QUESTION.format(self.num_one, self.operation, self.num_two)
def get_multiplication_question(self):
"""
:return: A multiplication question.
"""
self.prep_rand_num()
self.operation = "x"
return self.QUESTION.format(self.num_one, self.operation, self.num_two)
def get_division_question(self):
"""
:return: A division question.
"""
self.prep_rand_num()
self.operation = "%"
while self.num_two == 0 or self.num_one % self.num_two != 0:
self.prep_rand_num()
return self.QUESTION.format(self.num_one, self.operation, self.num_two)
def get_next_question(self, rand=False):
"""
:return: Next math question depending on the current operation.
"""
if rand:
_list = "+ - x %".split()
self.operation = _list[random.randint(0, len(_list) - 1)]
_dict = {
"+": self.get_addition_question,
"-": self.get_subtraction_question,
"x": self.get_multiplication_question,
"%": self.get_division_question
}
return _dict[self.operation]()
def get_answer(self):
"""
:return: Answer of question (Integer). Automatically knows for what operation.
"""
if self.operation == "+":
return self.num_one + self.num_two
elif self.operation == "-":
return self.num_one - self.num_two
elif self.operation == "x":
return self.num_one * self.num_two
else:
return self.num_one / self.num_two