-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion.py
133 lines (108 loc) · 4.13 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import random
import copy
class Question:
def __init__(self,database):
self.__data = json.load(database)
self.__questions = copy.deepcopy(self.__data)
self.__question = None
self.__points = 0
def drawQuestion(self, fromDb = False):
if fromDb:
self.__question = random.choice(self.__data["questions"])
else:
if len(self.__questions["questions"]) > 0:
idx = random.randrange(len(self.__questions["questions"]))
self.__question = self.__questions["questions"][idx]
self.__points += self.__question['points']
del self.__questions["questions"][idx]
else:
raise IndexError("There is no more questions to pick!")
def getAllQuestions(self):
questions = []
for i in self.__data["questions"]:
questions.append(i["question"])
return questions
def getRemainingQuestions(self):
questions = []
for i in self.__questions["questions"]:
questions.append(i["question"])
return questions
def getCount(self):
return len(self.__data["questions"])
def getRemainingCount(self):
return len(self.__questions["questions"])
def pickQuestionAt(self, idx):
self.__question = self.__data["questions"][idx]
def getQuestion(self):
return self.__question["question"]
def getCurrentIndex(self):
return self.__data["questions"].index(self.__question)
def getAllAnswers(self):
answers = []
for i in self.__question["answers"]:
answers.append(i["text"])
return answers
def getTrueAnswers(self):
answers = []
for i in self.__question["answers"]:
if i["isTrue"]:
answers.append(i["text"])
return answers
def getTrueAnswersFor(self,idx):
answers = []
for i in self.__data["questions"][idx]["answers"]:
if i["isTrue"]:
answers.append(i["text"])
return answers
def getAllAnswersFor(self,idx):
answers = []
for i in self.__data["questions"][idx]["answers"]:
answers.append(i["text"])
return answers
def checkIfAnswerIsTrue(self,answer):
for i in self.__question["answers"]:
if(i["text"] == answer):
return i["isTrue"]
return False
def checkIfAnswerIdIsTrue(self,answerId):
try:
if int(answerId) >= len(self.__question["answers"]):
return False
except ValueError:
return False
return self.__question["answers"][int(answerId)]["isTrue"]
def checkIfAnswerIsTrueFor(self,answer,idx):
for i in self.__data["questions"][idx]["answers"]:
if(i["text"] == answer):
return i["isTrue"]
return False
def checkIfAnswerIdIsTrueFor(self,answerId,idx):
try:
if int(answerId) >= len(self.__data["questions"][idx]["answers"]):
return False
except ValueError:
return False
return self.__question["answers"][int(answerId)]["isTrue"]
def isMultiAnswer(self):
hasCorrect = False;
for answer in self.__question["answers"]:
if not hasCorrect and answer['isTrue']:
hasCorrect = True
elif hasCorrect and answer['isTrue']:
return True
return False
def isMultiAnswerFor(self,idx):
hasCorrect = False;
for answer in self.__data["questions"][idx]["answers"]:
if not hasCorrect and answer['isTrue']:
hasCorrect = True
elif hasCorrect and answer['isTrue']:
return True
return False
def getAllAvailablePoints(self):
return self.__points
def getQuestionPoint(self):
return self.__question['points']
def getQuestionPointFor(self,idx):
return self.__data["questions"][idx]['points']