How to store and save correct answers with a set of digits as response in Digit span task?


How can we save and store key responses when doing a digit span task in which the correct answer

is a set of digits for one stimulus. for example, the column for the correct answer in CSV contains some digits as in the following:
[‘4’,‘9’, ‘7’, ‘2’]
[‘3’, ‘7’, ‘4’, ‘5’]
[‘5’, ‘9’, ‘6’, ‘0’]

  • Will there always be only four items to recall, or will the task difficulty vary across trials?

  • Do you want real-time feedback of the typed response or not?

First I want to have three or four items with four or three digits in the practice trial then I want to add more items with 5 to 11 digits in next trials to respond in backward order.

For example: 123 1236 12543 125469 12365478 123654354 two set for each trial with increasing digits.

Yes, I want real-time feedback of response also the typed response correct or incorrect in CSV output file, however, response accuracy is more important for my case.

OK, good.

Final question: how do you want to present the initial sequence for them to memorise? i.e. one digit at a time, or all together?

Yes, one digit at a time, digits of each set are presented individually in 1 second one after another, for example:

for the set or sequence: 12365 presentation: 1 2 3 6 5

OK. Your conditions file only needs a single column, like this:


Set up your task in Builder like this, with nested loops:

Connect the outer “trials” loop to your conditions file.

The inner loop is not connected to a conditions file. Set it to be sequential and put this in its nReps field, so that the loop will run as many times on each trial as there are digits to display:


i.e. this converts the number into a string of characters, breaks that up into a list of individual characters, and then counts how long that list is to know how many times to run the loop.

In the “learn” routine, insert a Code component (by clicking the “code” icon from the “custom” component panel). In its “begin routine” tab, put something like this:

# only need to do this once, on the first iteration of each trial:
if show_digits.thisN == 0:
    # convert the digits from a number into a string of characters then
    # break it into a list of individual characters:
    sequence = list(str(digits))
    correct_answer = list(reversed(sequence))

Insert a “Text” component into the “learn” routine, with a duration of 1 second. Put this in its “Text” field to display the digit corresponding to the the iteration number of the inner loop:


Set that text field to “set every repeat” so that it updates on every iteration.

On the “respond” routine, insert a keyboard component, set to not “force end of routine” and to store “All keys”. Also limit it to just accept the number keys. Also insert a text stimulus. Both of these components should have no fixed duration (so the routine will last indefinitely, until it is ended in code).

Again, insert a code component on this routine. In its “each frame” tab (so that this code runs on every screen refresh, for dynamic updating of the text stimulus, and keyboard responding), put something like this (NB the indenting levels are important, each level of indenting should be four spaces):

answer = your_keyboard_component_name.keys

# only score when the needed number of digits has been typed:
if len(answer) == len(correct_answer):

    thisExp.addData('answer', ''.join(answer))

    if answer == correct_answer:
        thisExp.addData('correct', 1)
        thisExp.addData('correct', 0)

    continueRoutine = False

In the text stimulus on that routine, put something like this to display the typed results as consecutive digits:


This time, set this text field to update via “set every frame” to capture the keypresses as they occur.

Now you might find that this ends a bit abruptly as soon as the last number is typed, so the person will hardly see the full response. Let me know if that is an issue and we can discuss how to address it.

Thank you. But I am wondering why error appears when naming the loop : Name must be…I understood. I should write just first parts.

Are you running this locally or online? It’s in Online experiments but @Michael has given you a solution that won’t work online without modification (see my crib sheet for thisN and join and I have no idea about reversed)

Yeah, running online.

In that case, for example.

thisExp.addData(‘answer’, ‘’.join(answer))

would need a manual translation in a Both component to something like

thisExp.addData(“answer”, answer.join(""));

Oops, missed that tag. But the general approach should work, with some tweaks to the Javascript.

I did all you said but faced the error ‘nReps is not defined’.

Can you provide the full text of the error message?

I faced this error. Could you please help me again here?

Does nReps appear in your code?

code in learn

code in respond

complete interface

I can’t see anything about your nReps there. Please could you show your loop definitions.

Also, you’ll need to switch code to Both and change show_digits to snapshot on the right hand side to fix .thisN online.

Where do you tell PsychoPy the value of nReps?

@Michael said put len(list(str(digits))) in the nReps field. However, for online use you’ll need to put nReps = len(list(str(digits))) in a Begin Routine of a code component that runs before you start the show_digits loop.

nReps for trials should probably be a number, e.g. 1