-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
50 lines (36 loc) · 1.22 KB
/
app.py
File metadata and controls
50 lines (36 loc) · 1.22 KB
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
import json
from flask import Flask, render_template, request
app = Flask(__name__)
# Define quiz questions and correct answers
with open('questions/beginner.json', 'r') as file:
topic1 = file.read()
questions = json.loads(topic1)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/learn')
def learn():
return render_template('learn.html')
@app.route('/quiz', methods=['GET', 'POST'])
def quiz():
if request.method == 'POST':
user_answers = {q: request.form.get(q) for q in questions}
results = {}
score = 0
for q_id, q_data in questions.items():
correct = q_data['answer']
user = user_answers.get(q_id)
is_correct = (user == correct)
if is_correct:
score += 1
results[q_id] = {
'question': q_data['question'],
'options': q_data['options'],
'correct': correct,
'user': user,
'is_correct': is_correct
}
return render_template('result.html', score=score, results=results)
return render_template('quiz.html', questions=questions)
if __name__ == '__main__':
app.run(debug=True)