我正在为一个项目开发一个图像游戏的答案。图像和正确答案存储在json文件的列表中。这些图像被随机渲染在游戏页面上,玩家在此输入答案。我正在尝试找到一种将用户答案与随机生成的图像进行比较的方法。以下是列表中的一些项目
{
"players": [
{
"image_source": "static/img/portfolio/neymar.jpg",
"answer": "Neymar"
},
{
"image_source": "static/img/portfolio/hugo_lloris.jpg",
"answer": Lloris"
},
{
"image_source": "static/img/portfolio/lorenzo_insigne.jpg",
"answer": "Insigne"
}
用户以html页面上的形式输入答案。由于图像是随机生成的,因此不像我希望的那样直接。
我最初的尝试是使用Jinja和隐藏形式:
<img class="img-fluid" {{ loop.index }}. src="{{ player.image_source }}" alt="player image">
<form method="POST">
<div class="control-group">
<div class="hidden_form">
<textarea rows="1"
id="question_number" name="question_number"
type="hidden" value="{{ loop.index }}">
</textarea>
</div>
<div class="hidden_form">
<textarea rows="1"
id="answer" name="answer"
type="hidden" value="{{ player.answer }}">
</textarea>
</div>
<div class="form-group
floating-label-form-group answers controls pb-2">
<textarea rows="1" class="form-control"
id="user_answer" name="user_answer"
type="text" placeholder="Enter Answer"
required="required"
data-validation-required-message="Please enter your answer.">
</textarea>
</div>
但是,我想避免使用隐藏的表单,因为我觉得这有点混乱。
我的Python代码是:
@app.route('/play', methods = ['GET', 'POST'])
def answers():
if session:
if request.method == "POST":
session['question_number'] +=1
session['guess'] = request.form.get("user_answer")
session['correct_answer'] = request.form.get("answer")
if session['guess'] == session['correct_answer']:
flash('Correct!')
else:
flash('Incorrect, please try again or skip to the next question')