Python Forum
Two Questions Regarding My Trivia Game
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Two Questions Regarding My Trivia Game
#1
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!
Reply
#2
Also, let me just say any help would be appreciated and thanks for letting me be a part of this forum! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New zealand Trivia Quiz SEWII 7 1,740 Mar-23-2023, 10:02 PM
Last Post: SEWII
  Questions about the GET and POST submission form Functions in Python for a Trivia Gam martin28 8 3,982 Aug-05-2018, 09:47 PM
Last Post: snippsat
  help setting questions and answers in quiz game? yoyoitsjess 3 3,654 May-10-2018, 07:35 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020