forked from UWOneLaptop/GeoAdventures
/
makeQuery.py
114 lines (94 loc) · 2.8 KB
/
makeQuery.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
import sqlite3
import random
#If you want to add stuff to db enter .schema for the
#schema to get attribute names in sqlite3.
class makeQuery:
#stuff for connecting sqlite3
con = sqlite3.connect('QandA.db')
c = con.cursor()
#initialize with country name and difficulty level.
def __init__(self, c, dl):
self.country = c
self.diffLevel = dl
self.questionList = self.getQList()
self.currQuestion = self.getQuestion()
self.answerList = self.getAList()
#Query the db
#returns allRows from the query.
#for internal use
def getQList(self):
t = (self.diffLevel, self.country, )
#gets all rows with same country and difficulty
self.c.execute('SELECT id, question FROM' + \
' questions WHERE level=? And name=?', t)
allRows = self.c.fetchall()
return allRows
#Query the db
#returns the list of possible answers to the current question
#for internal use.
def getAList(self):
qInfo = self.currQuestion
qid = qInfo[0]
t = (qid, )
self.c.execute('SELECT valid_ans, answer FROM'+ \
' answers WHERE qid=?', t)
allRows = self.c.fetchall()
return allRows
#retruns the current question as a String
def getQuestion(self):
index = random.random() * len(self.questionList)
return self.questionList[int(index)]
#generates new question in current difflevel and country
#catagory.
def newQuestion(self):
self.currQuestion = self.getQuestion()
self.answerList = self.getAList()
#finds the right answer from the list of answers.
def getRightAnswer(self):
rightAns = ""
for x in self.answerList:
if x[0] == 1:
rightAns = x[1]
return rightAns
#change the question topic, default level is 1.
def changeQuestionTopic(self, c):
self.country = c
self.changeQuestionLevel(1)
#change the question difficulty level.
def changeQuestionLevel(self, dl):
self.diffLevel = dl
self.questionList = self.getQList()
self.currQuestion = self.getQuestion()
self.answerList = self.getAList()
#tests if a country is present in the db. returns true if
#there is and false if there is not.
def isCountry(self, c):
t = (self.diffLevel, c, )
self.c.execute('SELECT DISTINCT name FROM' + \
' questions WHERE level=? And name=?', t)
allRows = self.c.fetchall()
if (len(allRows) == 0):
return 0
else:
return 1
def isLevel(self, dl):
t = (dl, self.country, )
self.c.execute('SELECT DISTINCT name FROM' + \
' questions WHERE level=? And name=?', t)
allRows = self.c.fetchall()
if (len(allRows) == 0):
return 0
else:
return 1
#returns current question
def getCurrQuestion(self):
return self.currQuestion[1]
#returns just the list of answers
def getAnswerList(self):
return self.answerList
#returns the current country
def getCountry(self):
return self.country
#returns the current difficulty level
def getLevel(self):
return self.diffLevel