Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

SQLite/Python Flask problem: I am making a trivia game in python flask. However, the variables message and score which I retrieve from my sqlite database

SQLite/Python Flask problem: I am making a trivia game in python flask. However, the variables message and score which I retrieve from my sqlite database show up weirdly on the site (see images and code below). How can I fix this?

On the website where the values of message and score should be you instead see

and

Score:

Python 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']]) message = cursor.execute('SELECT message FROM scores WHERE username = ?', [session['user']]); score = cursor.execute('SELECT score FROM scores WHERE username = ?', [session['user']]); return render_template('trivia.html', q=((data['results'])[a]['question']), a1=answer[0], a2=answer[1], a3=answer[2], a4=answer[3], score=score, message=message) @app.route('/checkAnswer/') def checkAnswer(): connection = sqlite3.connect('scores.db') cursor = connection.cursor() index1 = cursor.execute('SELECT index1 FROM scores WHERE username = ?', [session['user']]) score = cursor.execute('SELECT score FROM scores WHERE username = ?', [session['user']]) answer = cursor.execute('SELECT answer FROM scores WHERE username = ?', [session['user']]) if request.form['joke'] == index1: score += 100 message = "Correct!"  else: score -= 100 message = "Incorrect! The correct answer was " + answer cursor.execute('UPDATE scores SET score = ? message = ? WHERE username = ?', [score, 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

More Books

Students also viewed these Databases questions

Question

Solve the equation. 2 - 5/x = 3/x 2

Answered: 1 week ago

Question

What appraisal intervals are often used in appraisal reviews?

Answered: 1 week ago