-
Notifications
You must be signed in to change notification settings - Fork 9
/
question.py
62 lines (50 loc) · 1.84 KB
/
question.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
import random
from fractions import Fraction
class Question:
def __init__(self):
self.left_side = 0
self.right_side = 0
self.answer = 0
self.next()
def next(self):
self.populate_multiple_choice()
def populate_multiple_choice(self):
self.answer_index = random.randrange(4)
self.choices = []
for num in range(0,4):
if num == self.answer_index:
self.left_side = self.get_fraction()
self.right_side = self.get_fraction()
self.answer = self.left_side + self.right_side
while (
any(map(lambda x: x == self.answer, self.choices))
):
self.left_side = self.get_fraction()
self.right_side = self.get_fraction()
self.answer = self.left_side + self.right_side
self.choices.append(
self.answer
)
else:
choice = self.create_choice()
while (
any(map(lambda x: x == choice, self.choices))
):
choice = self.create_choice()
self.choices.append(
choice
)
def get_fraction(self):
den = random.randint(2,5)
num = random.randint(1,3)
while num % den == 0: num = random.randint(1,4)
return Fraction(num, den)
def create_choice(self):
return Fraction(self.get_fraction() + self.get_fraction())
def is_answer(self, fraction):
return fraction == self.answer
def __str__(self):
return '{left} + {right} = ?'.format(
left=self.left_side,
right=self.right_side
)