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)