vendredi 28 avril 2017

Why does flask-wtf not enter if statement?

I'm building a random-question quiz using Flask and flask-wtf. My quiz questions/answers are saved as the dict 'OceaniaQuestions' and a question is chosen at random to be entered into a form; its answer is paired by using the get function to get the answer from the dict.

def OceaniaQuiz():
print("Quiz loaded")

#Quiz dict and quiz properties
OceaniaQuestions = {'What is the name of the desert at 30-degrees South?':'Great Victoria Desert',
                    'What is the name of the island to the south east of Australia that is home to small "devils"?':'Tasmania',
                    'Which island nation is home to the Southern Alps?':'New Zealand',
                    'Which type of island chain contains a lagoon in the interior space between all of the individual islands?':'Atoll',
                    'Which type of island has rich soil for growing crops due to nutrients being brought to the surface of the Earth?':'Volcanic'}

question = random.choice(list(OceaniaQuestions.keys()))
correct_answer = OceaniaQuestions.get(question)

question_total = 0
quiz_length = len(OceaniaQuestions)

#Quiz form
class OceaniaContentForm(FlaskForm): 
    answer = StringField(question)
    correct_answer = OceaniaQuestions.get(question)

#Call form
form = OceaniaContentForm(question)
print("Question: {} | Answer: {}".format(question, correct_answer))
add_xp()
#response = make_response(render_template('OceaniaQuiz.html', form=form))

if form.validate_on_submit():
    print('At least got to the beginning of if-statement')
    user_answer = request.form.get('answer')
    #answer is incorrect
    if user_answer != correct_answer:
        #flash('Wrong!', 'error')
        return '<h1>Wrong!</h1>'
    else:
        #answer is correct
        OceaniaQuestions.pop(question)
        #question = random.choice(list(OceaniaQuestions.keys()))
        #correct_answer = OceaniaQuestions.get(question)
        return render_template('OceaniaQuiz.html', form=form)

return render_template('OceaniaQuiz.html', form=form)

Using the print functions, I know the code up until the 'if' statement is functioning properly, as the question and correct answer are returned to the terminal.

Why is the program not entering the 'if' statement and using its logic? When I enter the answer (both correct and incorrect) to a question, the program simply reloads a new random question and ignores the 'if' statement.

Thanks in advance for any help!

Aucun commentaire:

Enregistrer un commentaire