Flask Mini Quiz Application
Problem Statement
Program Question:
An educational website wants to develop an online multiple-choice quiz where students
answer a series of questions and get their final score at the end.
Build a mini Flask-based quiz app that shows questions, accepts answers,
and calculates the total score.
[Link]
from flask import Flask, render_template, request
app = Flask(__name__)
QUIZ_DATA = [
{"id": 1, "q": "What is 5 + 5?", "options": ["8", "10", "12"],
"ans": "10"},
{"id": 2, "q": "Capital of France?", "options": ["Berlin", "Madrid",
"Paris"], "ans": "Paris"}
]
@[Link]('/')
def quiz():
return render_template('[Link]', questions=QUIZ_DATA)
@[Link]('/submit', methods=['POST'])
def submit():
score = 0
for q in QUIZ_DATA:
user_answer = [Link](str(q['id']))
if user_answer == q['ans']:
score += 1
return f"<h1>Final Score: {score}/{len(QUIZ_DATA)}</h1> <a
href='/'>Restart</a>"
if __name__ == '__main__':
[Link](debug=True)
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Quiz App</title>
</head>
<body>
<h2>Simple Quiz</h2>
<form action="/submit" method="post">
{% for q in questions %}
<p><b>{{ q.q }}</b></p>
{% for opt in [Link] %}
<input type="radio" name="{{ [Link] }}" value="{{ opt }}"
required>
{{ opt }}<br>
{% endfor %}
<br>
{% endfor %}
<button type="submit">Finish Quiz</button>
</form>
</body>
</html>