'error list indices must be integers or slices, not str in flask

when I want to check the answers of a random question, flask debugger shows the error: list indices must be integers or slices, not str

my code:

from flask import Flask, render_template, flash, request, redirect, url_for
import random

app = Flask(__name__)
data = [{'q':'question text 1', 'ans':'a1', 'idsh':101}, {'q':'question text 2', 'ans':'a2', 'idsh':102}, {'q':'question text 3', 'ans':'a3', 'idsh':103}, {'q':'question text 4', 'ans':'a4', 'idsh':104}, ]

@app.route("/")
def home():
    return render_template('index.html', data = random.choice(data))


@app.route('/anss')
def answerd():
    if request.args.get('ans') == data['ans']:
        return "yes"
    else:
        return "no"

if __name__ == '__main__':
    app.run()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source