Use of textbox answer to code correct answer

OS (e.g. Win10): Mac Big Sur
PsychoPy version (e.g. 1.84.x): 20.2.10

What are you trying to achieve?:
I am trying to code correct answer (1/0) using the textbox answer of a certain task.

What did you try to make it work?:
I compare the answer given in the textbox to the actual stimulusalso see: Screen Shot 2021-01-20 at 8.30.57 AM

I use the following code:

if text == textbox_4.text:
score = score + 1
else:
score = score + 0

What specifically went wrong when you tried that?:
However, the score remains 1 even if the texts match. Does anyone have an idea what can go wrong here? The textbox_4 is defined under ‘ubentest’ and the text under ‘feedback’ in the same loop.

Screen Shot 2021-01-20 at 8.34.55 AM

Best regards, Sanne

Hi there,

Please can I check if you have seen this YouTube video? It sounds as though it may explain what you need :slightly_smiling_face: https://youtu.be/-Fto45M7bS0

Hope this helps,
Becca

Hi Becca,

Thanks! Yes that’s what I have, except that I only want to ‘document’ the score and do not give feedback directly during the task. However, when the score should be scored 1 (the textbox is the same as another variable called thisIm2 the score does not add up!

if thisIm2 == textbox_4.text:
score = score + 1
else:
score = score + 0

Also the output shows that the variables contain the same, but the score remains 0:

Screen Shot 2021-01-22 at 3.24.22 PM

Could it be possible there are some hidden spaces?

Also in which tab are you saving your “score” variable?

Hi Becca, thank you for answering!
No, so if I leave the textbox totally empty, I get the following message: AttributeError: ‘NoneType’ object has no attribute ‘replace’

But if I then put ‘antwort:’ in the textbox (or empty spaces) it works. However, the score still does not add up (so I create a new variable that combines antwort: with the answer; see below Screen Shot 2021-01-25 at 9.11.58 AM

I do not see what I’m missing here;

ubentest = where the participant has to type the word in the textbox and this word is stored
routine 2 = the correct word is combined with antwort so I can compare it to the answer
feedback_2: the answer is compared to the antwort and correct word (also see the picture from excel above and the code in the second picture)

Hi There,

Please could you share your files here so that we can take a look :slight_smile:

Thanks,
Becca

PAT.psyexp (152.7 KB) words.xlsx (13.4 KB) wordsu.xlsx (8.6 KB)

Yes here they are!

Hi There,

OK so for now I think this should do the trick - if you add a space to the text field of your textbox’s (this will avoid the attribute error you describe).

if textbox_4.text[0] ==" ":
    if thisIm2 == textbox_4.text[1:]:
         score = score + 1
    else:
        score = score + 0
else:
    if thisIm2 == textbox_4.text:
         score = score + 1
    else:
        score = score + 0

This essentially means if the firs letter is a space it will be ignored when calculating the accuracy.

Hi Becca,

Many thanks for your time. Unfortunately, I think this is not the main problem (I tried it of course), I earlier worked around this by putting ‘answer:’ in the textbox and then comparing the ‘answer:’ + what was typed in textbox_4 to thisIm2. However, the problem seems that even when the answers are correct (and also exactly the same shown in the output), the score still doesn’t add up. So this is how it looks with the space involved, same answer (when it ignores the space indeed), but score remains 0…

I really don’t understand, so it seems that the string variables in some way cannot really be compared?

Screen Shot 2021-01-28 at 4.35.53 PM

Hi There,

Aha OK I’ve looked into this more and think I have it. Currently there is a bug whereby text.text is replaced with “default text” at the end of the routine (the developers @TParsons are aware of this and it is fixed for the next release). For your case though this is causing problems, because the correct answer is not “default text” of course…

For now, you need to check if the correct answer has been typed in the textbox on each frame. I have added the code component “checkCorrect” to your file to demonstrate this - the value “score” saved to the output file now increases.
PAT.psyexp (158.2 KB)

Hope this helps,
Becca

Dear Becca,

Yes this solves the problem, thank you! It now gives the correct score.
The only problem that I have remaining now is that when I shuffle the stimuli en then let them come back in the second routine, the answers belonging to the stimuli are not shuffled with them, so I can’t check the correct score
Screen Shot 2021-02-01 at 9.59.27 AM
But maybe I just have to check this by hand and not automatize the scoring :slight_smile: (also in case participants make a typing error)

Hi there,

If you want to shuffle pairs of things i.e. stimuli and answers save a list of pairs i.e.

# start with your empty list
myList = []
# add two things to a list 
myList.append([stim, ans])
# shuffle the list
shuffle(myList)
# call the two things separately
thisStim = myList[0][0]
thisAns = myList[0][1]

Hope this helps!
Becca

I’ve got it all working now! Many thanks!!

Hi @Becca

I am currently encountering a similar problem! I have an experiment that worked in an early version locally (it works online), but now I get “default text” or nothing. In my case I have a free recall task where folks type in multiple responses. These responses are checked against a created dictionary. I have tried to move this out of the end routine portion but I still get the same issue. Do you know when this will be fixed, or maybe you have a work around?

Hi There!

Do you still get the issue if you move your component to the each frame tab?

The “default test” error has been fixed in the new release (which is due to be released imminently!)

Becca

Hi @Becca,

Placing the code in the each frame tab works. However, the problem is not fixed in the newest version.

Hi @jgeller112 thanks for flagging that - I’ll check it out

@jgeller112 I think the very simple change here fixes that issue. Would you like to test making that change in your own copy of PsychoPy and confirm?