# Use of textbox answer to code correct answer

**URL:** <https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770>\
**Category:** Builder\
**Tags:** textbox\
**Created:** [January 20, 2021, 7:35am UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770 "2021-01-20T07:35:47Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![suzannex](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@suzannex](https://discourse.psychopy.org/u/suzannex)\
**Post date:** [January 20, 2021, 7:35am UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/1 "2021-01-20T07:35:47Z")

</div>

**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](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/7/17a466534494988f5559499ced6d8fa55dca2107.png)

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](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/9/d/9d780d4b650d943720087f913d3cc658e9cbe3c2.png)

Best regards, Sanne

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [January 21, 2021, 10:34pm UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/2 "2021-01-21T22:34:58Z")

</div>

Hi there,

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

Hope this helps,  
Becca

---

<div class="post-metadata">

**Author:** ![suzannex](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@suzannex](https://discourse.psychopy.org/u/suzannex)\
**Post date:** [January 22, 2021, 2:25pm UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/3 "2021-01-22T14:25:36Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/f/8f02f60b7631930dc62f36f55a31176595a8950d.png)

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [January 22, 2021, 5:26pm UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/4 "2021-01-22T17:26:59Z")

</div>

Could it be possible there are some hidden spaces?

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

---

<div class="post-metadata">

**Author:** ![suzannex](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@suzannex](https://discourse.psychopy.org/u/suzannex)\
**Post date:** [January 25, 2021, 8:14am UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/5 "2021-01-25T08:14:24Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/c/ac011dfc39ab7522721a4951071abc94d01ace88.png)

I do not see what I’m missing here;

 ![Screen Shot 2021-01-25 at 9.12.50 AM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/5/8/584dc50035cad29eac38c1df4bfadb7cb2da7ee5.png)

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)

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [January 25, 2021, 9:43am UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/6 "2021-01-25T09:43:44Z")

</div>

Hi There,

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

Thanks,  
Becca

---

<div class="post-metadata">

**Author:** ![suzannex](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@suzannex](https://discourse.psychopy.org/u/suzannex)\
**Post date:** [January 25, 2021, 10:01am UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/7 "2021-01-25T10:01:39Z")

</div>

[PAT.psyexp](https://discourse.psychopy.org/uploads/short-url/sDSbQvxnJqqs8dhSW5s4fbLj43y.psyexp) (152.7 KB) [words.xlsx](https://discourse.psychopy.org/uploads/short-url/vFNdKHdYWy2KtEsndNtWRf6l96w.xlsx) (13.4 KB) [wordsu.xlsx](https://discourse.psychopy.org/uploads/short-url/kQjFFP3pgx3TEyMieV8049TMg4n.xlsx) (8.6 KB)

Yes here they are!

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [January 28, 2021, 1:32pm UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/8 "2021-01-28T13:32:30Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![suzannex](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@suzannex](https://discourse.psychopy.org/u/suzannex)\
**Post date:** [January 28, 2021, 3:36pm UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/9 "2021-01-28T15:36:06Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/f/1/f14b7977e4a1516498e01d91abd90d42b8c99bd9.png)

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [January 29, 2021, 11:21am UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/10 "2021-01-29T11:21:42Z")

</div>

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](https://discourse.psychopy.org/uploads/short-url/8zAdvb0RQ7DNlmjCeZM2Yk7cCj8.psyexp) (158.2 KB)

Hope this helps,  
Becca

---

<div class="post-metadata">

**Author:** ![suzannex](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@suzannex](https://discourse.psychopy.org/u/suzannex)\
**Post date:** [February 1, 2021, 8:59am UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/11 "2021-02-01T08:59:47Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/0/0/00630e11109b6cca01b1fe059e254bc2e285990e.png)  
But maybe I just have to check this by hand and not automatize the scoring 🙂 (also in case participants make a typing error)

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [February 1, 2021, 11:08am UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/12 "2021-02-01T11:08:13Z")

</div>

Hi there,

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

```auto
# start with your empty list
myList = []

```

```auto
# add two things to a list 
myList.append([stim, ans])

```

```auto
# shuffle the list
shuffle(myList)

```

```auto
# call the two things separately
thisStim = myList[0][0]
thisAns = myList[0][1]

```

Hope this helps!  
Becca

---

<div class="post-metadata">

**Author:** ![suzannex](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@suzannex](https://discourse.psychopy.org/u/suzannex)\
**Post date:** [February 1, 2021, 1:00pm UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/13 "2021-02-01T13:00:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jgeller112](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jgeller112/32/9835_2.png) [@jgeller112](https://discourse.psychopy.org/u/jgeller112)\
**Post date:** [February 14, 2021, 8:13pm UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/14 "2021-02-14T20:13:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [February 17, 2021, 9:24am UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/15 "2021-02-17T09:24:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jgeller112](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jgeller112/32/9835_2.png) [@jgeller112](https://discourse.psychopy.org/u/jgeller112)\
**Post date:** [February 23, 2021, 4:25pm UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/17 "2021-02-23T16:25:17Z")

</div>

Hi @Becca,

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

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [February 24, 2021, 11:46am UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/18 "2021-02-24T11:46:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [February 24, 2021, 2:25pm UTC](https://discourse.psychopy.org/t/use-of-textbox-answer-to-code-correct-answer/19770/19 "2021-02-24T14:25:34Z")

</div>

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

> <https://github.com/psychopy/psychopy/pull/3628/files>
