python: server is not receiving input from client flask

Jerry Thefilmmaker jerrythefilmmaker at gmail.com
Mon Jun 28 12:42:00 EDT 2021


I am new at python but newer at flask, and I'm trying to deploy a web app where I:
1. Send the user the question
2. Get the answer from the user
3. Send additional information based on the answer

I am able to send the question but stuck at getting the answer from the user. Base on the answer from the user the server returns a specific answer from a list of dict.

Here's my html:
<!DOCTYPE html>

<html>
<head>
    <title>THE QUIZ</title>
</head>

<body>
    <h1>QUIZ GAME</h1>

    <p>
        Welcome
    </p>

    <h2>Would you like to play</h2>

    <form action="/" method="POST">
            Enter your answer: <input type="text" name="answers">
             <input type="submit" value="submit">
    </form>

</body>
</html>

Here's my python code:
@app.route("/", methods = ['POST', 'GET']) 
def play():

    qa = questions.qa #list of dict with questions
    user1, user2 = [], [] #containers for each player's answers

    if request.method == 'POST':
        answers = request.form.get('answers')
        random.shuffle(questions.qa)
        response = quiz(qa, user1)

    return response 

def quiz(qa, user):
    for rand_q in qa:
        
        i = rand_q['question']
        i2 = check_answer(i, user)
        
    #print(rand_q["question"])
        return i2

@app.route("/", methods = ['POST', 'GET'])
def check_answer(ans, user):
    if request.method == 'POST':
        answers = request.form.get('answers')
        if 'yes' in answers:
            user.append('yes')
            i3 = ans.get(answers, '')#if the client answers "yes" to the question this part iterates over the part corresponds to yes in the list of dict

    return i3 #this should send 2nd part of question/rand_q

Can anyone help me see what I'm doing wrong, and how do I pass the 2nd answer from my list of dict to the client. Thanks!


More information about the Python-list mailing list