Check user input against json list strings












0















I am developing a guess the answer from an image game for a project. The images and correct answers are stored in a list in a json file. The images are randomly rendered on the game page, where the player inputs their answer. I am trying to find a way to compare the users answers with the randomly generated image. Below are some of the items in the list



{
"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"
}


The user enters the answer in a form on the html page. As the images are randomly generated this is not as straight forward as I hoped.



My initial attempt uses Jinja and hidden forms:



<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>


However, I would like to avoid using the hidden forms as I find this a little messy.



My Python code is:



@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')









share|improve this question




















  • 3





    What do you have so far? Did you break down the problem into smaller chunks yet? This, as is, is too broad.

    – Paritosh Singh
    Nov 13 '18 at 21:16











  • Try using pandas DataFrame to load the json file into your program. And then you can perform a lot of operations on that without having to use loops and all.

    – Sanchit Kumar
    Nov 13 '18 at 21:23
















0















I am developing a guess the answer from an image game for a project. The images and correct answers are stored in a list in a json file. The images are randomly rendered on the game page, where the player inputs their answer. I am trying to find a way to compare the users answers with the randomly generated image. Below are some of the items in the list



{
"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"
}


The user enters the answer in a form on the html page. As the images are randomly generated this is not as straight forward as I hoped.



My initial attempt uses Jinja and hidden forms:



<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>


However, I would like to avoid using the hidden forms as I find this a little messy.



My Python code is:



@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')









share|improve this question




















  • 3





    What do you have so far? Did you break down the problem into smaller chunks yet? This, as is, is too broad.

    – Paritosh Singh
    Nov 13 '18 at 21:16











  • Try using pandas DataFrame to load the json file into your program. And then you can perform a lot of operations on that without having to use loops and all.

    – Sanchit Kumar
    Nov 13 '18 at 21:23














0












0








0








I am developing a guess the answer from an image game for a project. The images and correct answers are stored in a list in a json file. The images are randomly rendered on the game page, where the player inputs their answer. I am trying to find a way to compare the users answers with the randomly generated image. Below are some of the items in the list



{
"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"
}


The user enters the answer in a form on the html page. As the images are randomly generated this is not as straight forward as I hoped.



My initial attempt uses Jinja and hidden forms:



<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>


However, I would like to avoid using the hidden forms as I find this a little messy.



My Python code is:



@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')









share|improve this question
















I am developing a guess the answer from an image game for a project. The images and correct answers are stored in a list in a json file. The images are randomly rendered on the game page, where the player inputs their answer. I am trying to find a way to compare the users answers with the randomly generated image. Below are some of the items in the list



{
"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"
}


The user enters the answer in a form on the html page. As the images are randomly generated this is not as straight forward as I hoped.



My initial attempt uses Jinja and hidden forms:



<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>


However, I would like to avoid using the hidden forms as I find this a little messy.



My Python code is:



@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')






python html json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 1:28









Mamoon Raja

42426




42426










asked Nov 13 '18 at 21:11









JonJon

83




83








  • 3





    What do you have so far? Did you break down the problem into smaller chunks yet? This, as is, is too broad.

    – Paritosh Singh
    Nov 13 '18 at 21:16











  • Try using pandas DataFrame to load the json file into your program. And then you can perform a lot of operations on that without having to use loops and all.

    – Sanchit Kumar
    Nov 13 '18 at 21:23














  • 3





    What do you have so far? Did you break down the problem into smaller chunks yet? This, as is, is too broad.

    – Paritosh Singh
    Nov 13 '18 at 21:16











  • Try using pandas DataFrame to load the json file into your program. And then you can perform a lot of operations on that without having to use loops and all.

    – Sanchit Kumar
    Nov 13 '18 at 21:23








3




3





What do you have so far? Did you break down the problem into smaller chunks yet? This, as is, is too broad.

– Paritosh Singh
Nov 13 '18 at 21:16





What do you have so far? Did you break down the problem into smaller chunks yet? This, as is, is too broad.

– Paritosh Singh
Nov 13 '18 at 21:16













Try using pandas DataFrame to load the json file into your program. And then you can perform a lot of operations on that without having to use loops and all.

– Sanchit Kumar
Nov 13 '18 at 21:23





Try using pandas DataFrame to load the json file into your program. And then you can perform a lot of operations on that without having to use loops and all.

– Sanchit Kumar
Nov 13 '18 at 21:23












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289553%2fcheck-user-input-against-json-list-strings%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289553%2fcheck-user-input-against-json-list-strings%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine