-
Notifications
You must be signed in to change notification settings - Fork 0
/
_trivia_class.py
80 lines (66 loc) · 2.29 KB
/
_trivia_class.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
from _database._database_class import RIMZDB
from random import choice
questionTableName = "QuestionItem"
choiceTableName = "ChoiceItem"
class trivia(RIMZDB):
def __init__(self, dbFile):
super(trivia, self).__init__(dbFile)
self.flag = False
self.itemNumber = 1
def getItems(self):
self.questionItems = {}
questions = self.getAllFromTable(questionTableName)
choices = self.getAllFromTable(choiceTableName)
try:
for question in questions:
temp = []
for choice in choices:
if question[0] == choice[1]:
temp.append(choice[2])
self.questionItems[question[0]] = (question[1], temp, question[2])
except TypeError:
return
def questionItemLength(self):
return len(self.questionItems)
def removeQuestionItem(self):
del self.questionItems[self.qID]
def resetNumber(self):
self.itemNumber = 1
def getQuestionItem(self):
questionItems = list(self.questionItems.keys())
self.qID = choice(questionItems)
def getQuestionDesc(self):
return self.questionItems[self.qID][0]
def getChoices(self):
letter = ["A", "B", "C", "D"]
choices = {}
for index in range(0, len(letter)):
choices[letter[index]] = self.questionItems[self.qID][1][index]
return choices
def getCorrectAnswer(self):
correctAnswer = self.questionItems[self.qID][2]
return self.convertIndexToLetter(correctAnswer)
def convertLetterToIndex(self, letter):
if letter in ["A", "a"]:
return 1
elif letter in ["B", "b"]:
return 2
elif letter in ["C", "c"]:
return 3
elif letter in ["D", "d"]:
return 4
def convertIndexToLetter(self, index):
if index == 1:
return "A"
elif index == 2:
return "B"
elif index == 3:
return "C"
elif index == 4:
return "D"
def checkAnswer(self, answer):
correctAnswer = self.questionItems[self.qID][2]
if self.convertLetterToIndex(answer) == correctAnswer:
return True
else:
return False