-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quiz.py
125 lines (101 loc) · 3.3 KB
/
Quiz.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
from flask import Flask, render_template, request
import os
import sys
import random
#reload(sys)
#sys.setdefaultencoding('utf8')
app = Flask(__name__)
quizSet = []
randRange = []
def readQuestion():
global quizSet
quizSet = []
data = None
my_dir = os.path.dirname(__file__)
filePath = os.path.join(my_dir, 'quiz.txt')
with open(filePath, 'r') as inputData:
data= inputData.read().split('\n')
questions = {}
count = 0
for line in data:
if (len(line)<=0):
break
if count%2 == 0:
if (len(line)<=1):
break
questions = {}
questions['Q'] = line
else:
questions['A'] = line
quizSet.append(questions)
count += 1
def getRand(ls):
return [I for I in range(len(ls))]
@app.route('/',methods=['POST','GET'])
def index():
return render_template('index.html')
@app.route('/start',methods=['POST','GET'])
def question():
global randRange
global quizSet
readQuestion()
randRange = [i for i in range(len(quizSet))]
questionNo = random.choice(randRange)
question = quizSet[questionNo]['Q']
attempted = 0
return render_template('display.html',score=0,questionNo=questionNo,status="",question=question,attempted=attempted+1)
@app.route('/question',methods=['POST','GET'])
def nextQuestion():
global randRange
global quizSet
questionNo = int(request.form['questionNo'])
score = int(request.form['score'])
answer = request.form['question']
attempted = int(request.form['attempted'])
ques = quizSet[questionNo]
if answer == ques['A']:
score +=1
ok = 'ok'
else:
ok = 'no'
rightanswer = ques['A']
return render_template('answer.html',score=score,questionNo=questionNo,status="",question=ques['Q'],attempted=attempted,answer= rightanswer, entered=answer, ok=ok)
@app.route('/answer',methods=['POST','GET'])
def nextAnswer():
global randRange
global quizSet
questionNo = int(request.form['questionNo'])
score = int(request.form['score'])
attempted = int(request.form['attempted'])
ok = request.form['ok']
attempted += 1
randRange.remove(questionNo)
if (ok == 'no'):
randRange.append(questionNo)
elif (len(randRange)<=0):
percent = int(100 * score / (attempted-1))
return render_template('score.html',score=score, attempted = attempted-1, percent=percent)
questionNo = random.choice(randRange)
ques = quizSet[questionNo]
return render_template('display.html',score=score,questionNo=questionNo,status="",question=ques['Q'],attempted=attempted)
@app.route('/edit',methods=['POST','GET'])
def edit():
global quizSet
readQuestion()
textareadata = ""
for question in quizSet:
textareadata += question['Q'] + '\n'
textareadata += question['A'] + '\n'
return render_template('edit.html', textareadata=textareadata)
@app.route('/editsubmit',methods=['POST','GET'])
def editsubmit():
textareadata = request.form['textareadata']
if(len(textareadata)<3):
return edit()
txtfile = open('quiz.txt', 'w')
n = txtfile.write(textareadata)
txtfile.close()
return render_template('index.html')
if __name__ == '__main__':
readQuestion()
app.run(host='0.0.0.0', debug=False)