45 – Real-World Python Projects – Online Quiz Platform

This project is a complete web-based quiz system where users can take quizzes, check scores, and get results instantly.
You’ll build backend + frontend + database using Python.


🎯 What You Will Learn

✔ Flask Web Framework
✔ HTML/CSS + Bootstrap
✔ Storing questions in JSON or SQLite
✔ Timer-based quiz
✔ Randomization of questions
✔ Auto-grading system
✔ Score history tracking
✔ Admin panel (optional)


📁 Project Folder Structure

online_quiz/
│── app.py
│── questions.json
│── templates/
│     ├── home.html
│     ├── quiz.html
│     ├── result.html
│── static/
      ├── style.css

🧩 1. Sample Questions File (questions.json)

[
  {
    "question": "What is the capital of France?",
    "options": ["London", "Paris", "Rome", "Berlin"],
    "answer": "Paris"
  },
  {
    "question": "Which language is used for AI?",
    "options": ["Python", "HTML", "CSS", "JavaScript"],
    "answer": "Python"
  },
  {
    "question": "Select the odd one out:",
    "options": ["Dog", "Cat", "Apple", "Lion"],
    "answer": "Apple"
  }
]

🧩 2. Flask Backend (app.py)

from flask import Flask, render_template, request
import json, random

app = Flask(__name__)

def load_questions():
    with open("questions.json", "r") as f:
        return json.load(f)

@app.route("/")
def home():
    return render_template("home.html")

@app.route("/quiz")
def quiz():
    questions = load_questions()
    random.shuffle(questions)
    return render_template("quiz.html", questions=questions)

@app.route("/result", methods=["POST"])
def result():
    questions = load_questions()
    score = 0
    total = len(questions)

    for i, q in enumerate(questions):
        user_ans = request.form.get(f"q{i}")
        if user_ans == q["answer"]:
            score += 1

    return render_template("result.html", score=score, total=total)

if __name__ == "__main__":
    app.run(debug=True)

🎨 3. HTML Templates

home.html

<!DOCTYPE html>
<html>
<head>
    <title>Online Quiz</title>
</head>
<body>
    <h1>Welcome to Online Quiz Platform</h1>
    <a href="/quiz">Start Quiz</a>
</body>
</html>

quiz.html

<!DOCTYPE html>
<html>
<head>
    <title>Take Quiz</title>
</head>
<body>
    <h2>Answer the following questions:</h2>

    <form action="/result" method="post">
        {% for q in questions %}
            <p><b>{{ loop.index }}. {{ q.question }}</b></p>

            {% for opt in q.options %}
                <input type="radio" name="q{{ loop.index0 }}" value="{{ opt }}" required>
                {{ opt }} <br>
            {% endfor %}
            <br>
        {% endfor %}

        <button type="submit">Submit</button>
    </form>
</body>
</html>

result.html

<!DOCTYPE html>
<html>
<head>
    <title>Quiz Result</title>
</head>
<body>
    <h1>Your Score: {{ score }}/{{ total }}</h1>

    {% if score == total %}
        <h2>Excellent! 🎉</h2>
    {% elif score > total/2 %}
        <h2>Good Job 👍</h2>
    {% else %}
        <h2>Keep Practicing 💪</h2>
    {% endif %}

    <a href="/">Go Home</a>
</body>
</html>

🚀 How to Run the Project

  1. Install Flask:
pip install flask
  1. Keep all files in the online_quiz folder.
  2. Run:
python app.py
  1. Open browser:
http://127.0.0.1:5000/

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *