Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am making a python flask trivia website. The following code shows the part of the program that shows the question, updates the score, and
I am making a python flask trivia website. The following code shows the part of the program that shows the question, updates the score, and shows a message. However, the checking function is not accurate. I'll show some examples below.
As you can see below, I got the answer right but it said it was incorrect. Also, the score isn't showing.
Python Flask Code:
@app.route('/trivia/' , methods=['GET', 'POST']) def trivia(): # Adds correct and incorrect answers to answer list and mixes them up a = random.randrange(1, 49) answer = [] answer1 = data['results'][a]['correct_answer'] for i in range(3): answer.append((data['results'])[a]['incorrect_answers'][i]) answer.append(answer1) random.shuffle(answer) answer_index = '' # Goes through mixed up list to find location of correct answer for i in range(4): if answer[i] == answer1: answer_index = 'an' + str((i + 1)) connection = sqlite3.connect('scores.db') cursor = connection.cursor() cursor.execute('UPDATE scores SET answer = ?, index1 = ? WHERE username = ?', [answer1, answer_index, session['user']]) connection.commit() cursor.execute('SELECT message FROM scores WHERE username = ?', [session['user']]); message = cursor.fetchone() cursor.execute('SELECT score FROM scores WHERE username = ?', [session['user']]); score = cursor.fetchone() return render_template('trivia.html', q=((data['results'])[a]['question']), a1=answer[0], a2=answer[1], a3=answer[2], a4=answer[3], score=score[0], message=message[0]) @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']]) connection.commit() return redirect(url_for('trivia'))
HTML Code:
{% 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: {{score1}}h3> div> {% endblock %}What is the name of Poland in Polish? Polszka Pupcia O Polska Pó:land submit Incorrect! The correct answer was Spruce Goose Score
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started