score keeping issue in flask trivia game

  • A+
Category:Languages

I've been making progress but having some difficulty with two things here's my code:

Python Code:

import os from flask import Flask, render_template, request, redirect, url_for  score = 0 score2 = 0  app = Flask(__name__)  class Question:     def __init__(self, prompt, answer):         self.prompt = prompt         self.answer = answer  questions = [     Question([0], "Python"),     Question([1], "Paris"),     Question([2], "Moonlight"),     Question([3], "Lemonade"),     Question([4], "Yes"),     Question([5], "Call Me By Your Name"),     Question([6], "Engels & Marx"),     Question([7], "Freud"),     Question([8], "Gordon Ramsay")  ]  @app.route('/') def quiz():     return render_template('index.html')  @app.route('/quiz', methods=['POST'])     def quiz_answers():     for Question in questions:         answer = request.form['language']         global score         if answer == Question.answer:             score = score+1             return render_template('capitals.html',data=score)      return render_template("index.html") + answer + "<h4>is not correct, guess again.</h4>"  @app.route('/question', methods=['POST']) def question_two():     for Question in questions:         answer = request.form['capital']         global score         if answer == Question.answer:             score = score+1             return render_template('movies.html',data=score)     return render_template('capitals.html', data=score) + answer + "<h4>is not correct, guess again.</h4>"  @app.route('/movie', methods=['POST']) def question_three():     for Question in questions:         answer = request.form['movie']         if answer == Question.answer:             global score             score += 1             return render_template('music.html',data=score)     return render_template("movies.html", data=score) + answer + "<h4>is not correct. Wanna guess again</h4>"   @app.route('/music', methods=['POST']) def question_four():     for Question in questions:         answer = request.form['album']         if answer == Question.answer:             global score             score += 1             return render_template('results.html',data=score)     return render_template("music.html", data=score) + answer + "<h4>is not correct, guess again.</h4>"  @app.route('/ready', methods=['POST']) def results1():     for Question in questions:         answer = request.form['ready']         if answer == Question.answer:             return render_template('novel.html')     return render_template('novel.html')   @app.route('/novel', methods=['POST']) def question5():     for Question in questions:         answer = request.form['novel']         if answer == Question.answer:             global score2             score2 += 1             return render_template('history.html',data2=score2)     return render_template('novel.html',data2=score2) + answer + "<h4>is not correct, guess again.</h4>"  @app.route('/history', methods=['POST']) def question6():     for Question in questions:         answer = request.form['history']         if answer == Question.answer:             global score2             score2 += 1             return render_template('psychology.html',data2=score2)     return render_template('history.html',data2=score2) + answer + "<h4>is not correct, guess again.</h4>"  @app.route('/psychology', methods=['POST']) def question7():     for Question in questions:         answer = request.form['psychology']         if answer == Question.answer:             global score2             score2 += 1             return render_template('cuisine.html',data2=score2)     return render_template('psychology.html',data2=score2) + answer + "<h4>is not correct, guess again.</h4>"  @app.route('/cuisine', methods=['POST']) def question8():     for Question in questions:         global score         answer = request.form['cuisine']         if answer == Question.answer:             global score2             score2 += 1             return render_template('results2.html',data2=score2, data=score)     return render_template('cuisine.html',data2=score2) + answer + "<h4>is not correct, guess again.</h4>"  if __name__ == '__main__':     app.run(host=os.environ.get('IP'),     port=int(os.environ.get('PORT')),     debug=True) 

First thing: I'd like to able to return not only my str(score), but also redirect to my capitals.html page using a redirect(url_for('java')). Is it possible to return both? I know I can't use the + sign because python interprets it as my adding the two statements together, and I know I can't return twice, is there a way that I can redirect to another page and display my score as a string on that page? It's probably a simple solution, I'm sure, but I'm not really understanding it.

And secondly, Is there a way to redirect or route from one page to another and still keep track of score without having to define it every page? For example, could I answer one question correctly in index.html and carry that +1 score over to my capitals page? Would really appreciate it if somebody could give me some tips. Thanks so much guys

 


To add a highscore feature you can simply create a text file in python using this syntax:

f = open('highscore.txt',r) 

Then similarly to how I explained before create a global variable for the username. When the questions are answered you can simply check the score in the file against the current score. Lets say the file has the score in a format as such name:score. To parse this we should:

currenthigh = int(f.readline().split(':')[1]) f.close() global score global name if (score > currenthigh):    f = open('highscore.txt',w+)    f.write("{}:{}".format(name,str(score)) 

I hope that this helps otherwise I will write it out more in detail.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: