Randomized n-back with fixed number of targets not displaying stimuli (workshop demo missing info)

Hello all,

I’ve been working off of this tutorial to create an n-back with randomized stimuli in the PsychoPy builder with custom code; but this example doesn’t include all of the script needed.

Essentially, it is supposed to select a random letter from a list to present, but select the same letters as n items back whenever it is supposed to be a ‘target’ trial (based on a spreadsheet used in a loop). The tutorial code uses np.random.choice() which doesn’t work for javascript, so they say to use shuffle(). But this isn’t shown in the example, nor is the code for displaying the randomly selected letter.

After much trial and error and finding something approximating the missing code in this experiment, the code runs but it doesn’t actually display the randomly selected letter.

While I could just make everything based off of a fixed spreadsheet, I would rather participants did not always see the same stimuli in the same order.
I also cannot make this fully random because each participant needs to see a certain number of ‘target’ stimuli in order for scores to be comparable.

So far the python code looks like this (for a 1-back condition):
(in ‘begin experiment’)

letters = [‘B’,‘F’,‘K’,‘M’,‘Q’,‘R’,‘X’]
presentedLetters1back =

(in ‘begin routine’)

n = 1 # specify the n condition
letterSelected = False # a boolean to state a letter has not yet been selected
thisLetter = “0” # predefine the letter to zero based. Note that changing this does not make the stimuli appear.

if not target: # if this is not a target trial
while not letterSelected:
thisLetter = letters.slice((-1))[0];
if len(presentedLetters1back) < n or thisLetter != presentedLetters1back[-n]:
# if n letters have not been presented or this is not the same as the nth trial back
letterSelected = True

presentedLetters1back.append(thisLetter); # add to list of presented stimuli
stimulustext_1back.setText(thisLetter); # add randomly selected letter to text component

Things I’ve tried and other settings:

  • defining thisLetter with other randomization methods including Math.random().
  • shuffling letters to a new variable and then selecting thisLetter as the first item from that variable
  • the code component is the first component for the routine in builder view.
  • the stimulustext_1back is set to “set every repeat”.
  • I am not getting any errors when psychopy automatically translates this to javascript
  • setting the text to thisLetter.toString().
  • I cannot find the accompanying files to this demo.

I also don’t know how to check if thisLetter is even being defined properly. I can’t run it in the shell of the coder window because I don’t know how to get psychopy to add in until. or math. for javascript.

Hi Chelsie,

Did you ever find a resolution for this issue? I have the same problem!


Hello all,

I’m working on the same task now

Did anybody find a solution for it? I found a new discussion but still I can’t decide this problem

Thanks in advance


to solved this, altering the following:
“Begin Experiment Tab”:

letters = ['B', 'F', 'K', 'M', 'Q', 'R', 'X']

presentedLetters = []

You do not need to extra assign thisLetter.

“Begin Routine Tab”:

n = 1
letterSelected = False
if not target:
    while not letterSelected:
        thisLetter = letters[-1]
        if len(presentedLetters) < n or thisLetter != presentedLetters[-n]:
            letterSelected = True
    thisLetter = presentedLetters[-n]


I changes the letters.slice to letters[-1], which does the same thing, but does not cause an error for me, while slice did not work (Error: "List object has no attribute ‘slice’).

Be sure to set in the Text-Component $thisLetter to set every repeat.
Hope that helps:)!

Hello again

I spent some time to find a solution for this problem. I would like to share a code from github. It creates 1, 2, 3 back sequence which can be used as trials sequentially. Although it is necessary to edit for your tasks and to create 0 back condition (in future maybe I’ll add my version).

To add it in Condition you can use this line $(‘0back_id_’+str(expInfo[‘participant’])+‘.csv’)

However, it’s not the best solution. Probably it’s possible to add in code components, I hope that somebody will do it :slightly_smiling_face:

I hope it helps