Python Forum

Full Version: Two Questions Regarding My Trivia Game
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys, I've been making progress since last I posted about my flask trivia game, but having some difficulty with two things here's my code:

import os
from flask import Flask, render_template, request, redirect, url_for
 
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")
    
]
    
@app.route('/')
def quiz():    
    return render_template('index.html')
    
@app.route('/quiz', methods=['POST'])    
def quiz_answers():
    score = 0
    for question in questions:
        answer = request.form['language']
        if answer == question.answer:
            score += 1
            return str(score) + redirect(url_for('python'))     
            
@app.route('/python')
def python():
    '''Example of redirect'''
    return render_template('capitals.html')  
            
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.

Also, a very special thank you to snippsat for helping me out with my previous problem. Thank you so much, again!
Also, let me just say any help would be appreciated and thanks for letting me be a part of this forum! :)