File size: 5,012 Bytes
644c189
 
 
 
48d333f
644c189
48d333f
 
 
 
 
 
 
 
 
 
 
644c189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from flask import Flask, render_template, request, redirect, url_for, session
import random
import os
import time

app = Flask(__name__)

# 1. Update the secret key to be more secure
app.secret_key = os.environ.get("SECRET_KEY", "math_quiz_default_key_123")

# 2. Add this block to fix the "Resetting to Defaults" issue
app.config.update(
    SESSION_COOKIE_SAMESITE='None',
    SESSION_COOKIE_SECURE=True
)

# ... the rest of your functions (generate_question, etc.) follow below


def generate_question(low, high, operation):
    if operation == "Add":
        num1 = random.randint(low, high)
        num2 = random.randint(low, high)
        correct_answer = num1 + num2
        question = f"{num1} + {num2}"
        symbol = "+"
    elif operation == "Subtract":
        num1 = random.randint(low, high)
        num2 = random.randint(low, high)
        if num1 < num2:
            num1, num2 = num2, num1
        correct_answer = num1 - num2
        question = f"{num1} - {num2}"
        symbol = "-"
    elif operation == "Divide":
        num2 = random.randint(max(1, low), high)
        answer = random.randint(low, high)
        num1 = num2 * answer
        correct_answer = answer
        question = f"{num1} ÷ {num2}"
        symbol = "÷"
    else:  # Multiply
        num1 = random.randint(low, high)
        num2 = random.randint(low, high)
        correct_answer = num1 * num2
        question = f"{num1} × {num2}"
        symbol = "×"
    return num1, num2, correct_answer, question, symbol


@app.route("/", methods=["GET", "POST"])
def configure_quiz():
    if request.method == "POST":
        session.clear()

        session["low"] = int(request.form["low"])
        session["high"] = int(request.form["high"])
        session["num_questions"] = int(request.form["num_questions"])
        session["operation"] = request.form.get("operation", "Multiply")

        session["questions"] = []
        session["correct_answers"] = 0
        session["total_attempts"] = 0
        session["start_time"] = time.time()

        difficulty = request.form.get("difficulty", "Medium")
        time_limits = {"Easy": 30, "Medium": 20, "Hard": 10}
        session["difficulty"] = difficulty
        session["time_per_question"] = time_limits.get(difficulty, 20)

        return redirect(url_for("quiz"))

    return render_template("configure_quiz.html")


@app.route("/quiz", methods=["GET", "POST"])
def quiz():
    if "low" not in session or "high" not in session or "num_questions" not in session:
        return redirect(url_for("configure_quiz"))

    low = session["low"]
    high = session["high"]
    num_questions = session["num_questions"]
    operation = session.get("operation", "Multiply")

    if "questions" not in session:
        session["questions"] = []

    if session["total_attempts"] >= num_questions:
        return redirect(url_for("summary"))

    if request.method == "POST":
        user_answer = request.form.get("answer")
        correct_answer = session["correct_answer"]

        session["total_attempts"] += 1
        is_correct = user_answer.isdigit() and int(user_answer) == correct_answer

        if user_answer == "TIMEOUT":
            is_correct = False
            user_answer = None

        session["questions"].append({
            "num1": session["num1"],
            "num2": session["num2"],
            "question": session["question"],
            "correct_answer": correct_answer,
            "user_answer": int(user_answer) if user_answer is not None and user_answer.isdigit() else None,
            "is_correct": is_correct
        })

        if is_correct:
            session["correct_answers"] += 1

        if session["total_attempts"] >= num_questions:
            return redirect(url_for("summary"))

    num1, num2, correct_answer, question, symbol = generate_question(low, high, operation)

    session["num1"] = num1
    session["num2"] = num2
    session["question"] = question
    session["correct_answer"] = correct_answer
    session["symbol"] = symbol

    return render_template(
        "quiz.html",
        num1=num1,
        num2=num2,
        symbol=symbol,
        correct_answer=correct_answer,
        correct=session.get("correct_answers", 0),
        total=session.get("total_attempts", 0),
        num_questions=num_questions
    )


@app.route("/summary")
def summary():
    end_time = time.time()
    session["total_time"] = round(end_time - session.get("start_time", end_time))

    return render_template(
        "summary.html",
        low=session["low"],
        high=session["high"],
        num_questions=session["num_questions"],
        questions=session.get("questions", []),
        correct=session.get("correct_answers", 0),
        total=session.get("total_attempts", 0),
        total_time=session["total_time"],
        operation=session.get("operation", "Multiply"),
        difficulty=session.get("difficulty", "Medium")
    )


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=5000)