Question: I am making a trivia game website using python flask. The following code shows the questions, lets the user submit it, and checks the accuracy.

I am making a trivia game website using python flask. The following code shows the questions, lets the user submit it, and checks the accuracy. It is supposed to update the score, however, the score remains at 0 and the message never changes. How can I fix this?

Python Flask Code:

@app.route('/checkAnswer', methods=['GET', 'POST']) def checkAnswer(): connection = sqlite3.connect('scores.db') cursor = connection.cursor() cursor.execute('SELECT index1 FROM scores WHERE username = ?', [session['user']]); index1 = cursor.fetchone() cursor.execute('SELECT score FROM scores WHERE username = ?', [session['user']]); score = cursor.fetchone() cursor.execute('SELECT answer FROM scores WHERE username = ?', [session['user']]); answer = cursor.fetchone() score1=int(score[0]) if request.form['joke'] == index1: score1 += 100 message = "Correct!"  else: score1 -= 100 message = "Incorrect! The correct answer was " + answer[0] cursor.execute('UPDATE scores SET score = ?, message = ? WHERE username = ?', [score1, message, session['user']]) return redirect(url_for('trivia'))

HTML:

{% extends "layout.html" %} {% block content %} <div class="trivia"> <form action="/checkAnswer" method="post"> <h1>{{ q }}h1> <div class="form-group"> <input type="radio" name="joke" value="an1" /> {{ a1 }} br> <input type="radio" name="joke" value="an2" /> {{ a2 }} br> <input type="radio" name="joke" value="an3" /> {{ a3 }} br> <input type="radio" name="joke" value="an4" /> {{ a4 }} br> div> <input class="btn btn-primary" type="submit" value="submit"> form> <h3>{{message}}h3> <h3>Score: {{score}}h3> div> {% endblock %}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!