Aug-04-2018, 07:37 PM
Hey guys! I'm Martin and I'm new to the forum. I was wondering if you could answer my question and possibly solve it for me because I'm really getting depressed over not being able to do it. I'm taking a software development course at the moment and I'm stuck on a Python related assignment. Any and all help would be amazing.
Let me explain my problem: I'm building a trivia game using Python for an assignment. It's being built with a Flask application so that I can open my Python/HTML in a browser (make it more user friendly). My quiz is a multiple choice question quiz, however, I need to create it using a submission form style of answering, not a multiple choice button. I'm using a template I found online for now until I understand the full in and outs but I need to ask: How do I use the GET and POST functions to allow a user to answer the question in a form in the browser and send their answer through my Python logic (the if or else statements) to send them back either a correct or incorrect response?
To clarify, what I would like to be able to do with the code below is get rid of my multiple choice answer system and instead have a system where the user can submit their answer through a form and the logic will return them with a correct/incorrect answer. I'm not plagiarising this code for my assignment, I'm simply using it as a template to trying and explain myself better. Thanks.
<h1>7 Wonders of the World</h1>
<form action='/quiz' method='POST'>
<ol>
{% for i in q %}
<li>Where is <u>{{i}}</u> located?</li>
{% for j in o[i] %}
<input type='radio' value='{{j}}' name='{{i}}' />{{j}}
{% endfor %}
{% endfor %}
</ol>
<input type="submit" value="submit" />
</form>
</html>
EDIT ----- Sincerest apologies, I'm not sure how the indent works on this forum. Apologies again.
Let me explain my problem: I'm building a trivia game using Python for an assignment. It's being built with a Flask application so that I can open my Python/HTML in a browser (make it more user friendly). My quiz is a multiple choice question quiz, however, I need to create it using a submission form style of answering, not a multiple choice button. I'm using a template I found online for now until I understand the full in and outs but I need to ask: How do I use the GET and POST functions to allow a user to answer the question in a form in the browser and send their answer through my Python logic (the if or else statements) to send them back either a correct or incorrect response?
To clarify, what I would like to be able to do with the code below is get rid of my multiple choice answer system and instead have a system where the user can submit their answer through a form and the logic will return them with a correct/incorrect answer. I'm not plagiarising this code for my assignment, I'm simply using it as a template to trying and explain myself better. Thanks.
from flask import Flask, render_template, request import random, copy app = Flask(__name__) original_questions = { #Format is 'question':[options] 'Taj Mahal':['Agra','New Delhi','Mumbai','Chennai'], 'Great Wall of China':['China','Beijing','Shanghai','Tianjin'], 'Petra':['Ma\'an Governorate','Amman','Zarqa','Jerash'], 'Machu Picchu':['Cuzco Region','Lima','Piura','Tacna'], 'Egypt Pyramids':['Giza','Suez','Luxor','Tanta'], 'Colosseum':['Rome','Milan','Bari','Bologna'], 'Christ the Redeemer':['Rio de Janeiro','Natal','Olinda','Betim'] } questions = copy.deepcopy(original_questions) def shuffle(q): """ This function is for shuffling the dictionary elements. """ selected_keys = [] i = 0 while i < len(q): current_selection = random.choice(q.keys()) if current_selection not in selected_keys: selected_keys.append(current_selection) i = i+1 return selected_keys @app.route('/') def quiz(): questions_shuffled = shuffle(questions) for i in questions.keys(): random.shuffle(questions[i]) return render_template('main.html', q = questions_shuffled, o = questions) @app.route('/quiz', methods=['POST']) def quiz_answers(): correct = 0 for i in questions.keys(): answered = request.form[i] if original_questions[i][0] == answered: correct = correct+1 return '<h1>Correct Answers: <u>'+str(correct)+'</u></h1>' if __name__ == '__main__': app.run(debug=True)<html>
<h1>7 Wonders of the World</h1>
<form action='/quiz' method='POST'>
<ol>
{% for i in q %}
<li>Where is <u>{{i}}</u> located?</li>
{% for j in o[i] %}
<input type='radio' value='{{j}}' name='{{i}}' />{{j}}
{% endfor %}
{% endfor %}
</ol>
<input type="submit" value="submit" />
</form>
</html>
EDIT ----- Sincerest apologies, I'm not sure how the indent works on this forum. Apologies again.