CMII Quiz for Self Study – Flask Project

This Flask web application implements a simple quiz with multiple-choice questions related to the Certificate of the Malaysian Insurance Institute (CMII) final exam. Let’s break down the code:

Imports

from flask import Flask, render_template, request, session, redirect, url_for

import random

The code imports the necessary modules for building a web application using Flask and for handling randomization.

Flask Setup

app = Flask(__name__)

app.secret_key = 'your_secret_key'  # Change this to a secure secret key

This creates a Flask application instance and sets a secret key for session management. The secret key is used to sign session cookies for security.

Quiz Data

quiz_data = [
    # ... (dictionaries containing quiz questions, options, and correct answers)
]

A list of dictionaries containing quiz questions, answer options, and correct answers.

Route: /

@app.route('/')

def index():

    # Shuffle the quiz_data list to randomize the questions
    random.shuffle(quiz_data)

    # Select the first 10 questions from the shuffled list
    selected_questions = quiz_data[:10]

    # Store the selected questions in the session
    session['selected_questions'] = selected_questions

    return render_template('quiz.html', quiz_data=selected_questions)

This route is the home page. It shuffles the quiz questions, selects the first 10, stores them in the session, and renders the ‘quiz.html’ template.

Route: /quiz

@app.route('/quiz', methods=['POST'])

def quiz():

    # ... (code to evaluate the quiz and calculate the score)

    return render_template('score.html', score=score, total=len(selected_questions), incorrect_responses=incorrect_responses)

This route handles the form submission from the quiz page. It compares user responses with correct answers, calculates the score, and renders the ‘score.html’ template with the results.

Main Block

if __name__ == '__main__':

    app.run(debug=True)

This block ensures that the Flask app is only run when the script is executed directly, not when it’s imported as a module. It runs the app in debug mode.

Conclusion

Overall, the application provides a simple web-based quiz where users can answer multiple-choice questions, and it calculates and displays their score at the end. The quiz questions are randomly selected from the predefined set.

The Flask app is then deployed on PythonAnywhere for public use! All I need to do is to log into PythonAnywhere at least once every three months and click the “Run until 3 months from today” button to keep the web app live. In case the link seems to expire, kindly send me a message to run app and then the quiz should work.

If you are a CMII student or someone who is planning to sit for the 301 Insurance Principles & Market Practice subject, feel free to do your revision on the quiz I created based on sample questions! Good luck!

Please follow and like us:

Latest Posts