0

enter image description here

Here Im creating a trivia quiz with python, flask, html. I'm having trouble checking/ giving the results. I've my list of questions and answers stored in a sql database called questions.

this is my code to print out the list of questions on questionpage.html. (quiz.html is where i get users to input how many qns/ category. Look at A in code.

but i'm not sure how to mark the quiz show what is incorrect/correct. i created a new html page called check.html. and i tried to loop through each of the question in quizquestions, but the thing is /questionpage router is a different router from /quiz and hence i can't assess the variable quizquestions. Look at B in code.

A.

@app.route("/quiz", methods=["GET", "POST"])
def quiz():
    if request.method == "POST":
        chosencategory = request.form.get("chosencategory")
        number = int(request.form.get("number"))
        quizquestions = db.execute("SELECT Question FROM questions WHERE Category = ? ORDER BY RANDOM() LIMIT ?", chosencategory, number)
        return render_template("questionpage.html", quizquestions = quizquestions)

    else:
        genres = db.execute("SELECT DISTINCT Category FROM questions ORDER BY Category")
        return render_template("quiz.html", genres = genres)

B.

@app.route("/questionpage", methods=["GET", "POST"])
def questionpage():
    if request.method == "POST":
        scores = 0
        correct = 0
        for quizquestion in quizquestions:
            questionline = quizquestion.Question
            modelanswers = db.execute("SELECT Answer FROM questions WHERE Question = ?", questionline)
            answer = request.form.get("answer").capitalise
            if answer == modelanswers:
                score += 1
                correct = 1
        totalscore = scores
        return render_template("check.html", quizquestions = quizquestions, modelanswers = modelanswers)
    else:
        return render_template("questionpage.html")

html for /quiz

<form action="/quiz" method="post">
    <div class="form-group">
        <div class="form-group">
        <select name = "chosencategory">
            <option disabled selected value="">Category</option>
            {% for genre in genres %}
                <option value = "{{genre.Category}}">{{genre.Category}}</option>
            {% endfor %}
        </select>
    </div>
    </div>
        <input autocomplete="off" autofocus class="form-control" name ="number" placeholder="Number of questions" type="text">
    </div>
    <p></p>
    <div class="form-group">
    <button class="btn btn-primary" type="submit">Submit</button>
    </div>
</form>

html for /questionpage

<form action="/questionpage" method="post">
    <div class="form-question">
        {% for quizquestion in quizquestions %}
        <p>
                <p name = question class = "questionline">{{quizquestion.Question}}</p>
                <p></p>
                <input autocomplete="off" autofocus class="form-control" name ="answer" placeholder="Answer" type="text">
        </p>
        {% endfor %}
    </div>
    <button class="btn btn-primary" type="submit">Submit</button>
</form>
noobcs
  • 9
  • 2

1 Answers1

2

You could store the current questions, which a user has to answer in a session-like object. For more reference I found this thread: Using variables across Flask routes

I can also recommend to use an ORM like sqlalchemy for your SQL queries, since that highly simplifies your work regarding that matter.

jpaodev
  • 136
  • 5