# Presenting random sequence of digits each trial

OS : Windows 10
PsychoPy version : v2021.2.3
What are you trying to achieve?:
This is my first time using python/psychopy builder so apologies if I’m missing some basic knowledge that would help me get started with this. I want to set up an experiment in which random sequences of 5 digits are presented on screen in each trial (n trials = 40), and then in a later part of each trial, keyboard input is checked for a match to the digits that were presented. I need the sequences to be shuffled for each participant (ie., not just randomised initially and presented to each participant in the same order).

If anyone has some advice for how this could be set up I would greatly appreciate it.

Thanks

Hi

Search for online demos to find my digit span demo which generates sequences which start with 1-9 and then follows with random digits 0-9 with no doubles.

Hi There,

There are many possible approaches but here are some handy functions and I believe wakes demo will show a nice implementation.

To outline some of the basic functions to get started. You could have a spreadsheet with the column header “digits” and each row value corresponding to the digits e.g.:

## Digits

1,2,3,4,5
7,8,9,5,6
2,3,4,3,4

Add two routines to your flow with a loop around them that takes this spreadsheet. In the first routine, add a code component and in the “Begin Routine” tab use:

``````Digits.split(",") # make a list of numbers
shuffle(Digits) # shuffle the digits
nDigits = len(Digits) # check how many digits
thisExp.addData('new_digits', Digits) # store the shuffled digits to data file
``````

For the second routine, add a loop around it (on the inside of the first loop) and in the `nReps` field use `nDigits`. That routine should contain a text component that has the text field set to “set every repeat” with the text field set to `\$Digits[innerloopname.thisN]`

Hope this helps
Becca

1 Like

Thank you, I’m giessing it is the one on this page? PsychoPy Online Demos

Correct.

Thank you!

I tried setting it up as you described, but got this error message:

``````TypeError: 'str' object does not support item assignment
##### Experiment ended. #####
``````

I’m guessing it is because of this line? :

`thisExp.addData('new_digits', Digits) # store the shuffled digits to data file`

Is this a local error or online?

Please could you show the current code you are using? I don’t think that’s the line with the error.

I’m running it locally, I put this code into the ‘begin routine’ tab:

``````Digits.split(",") # make a list of numbers
shuffle(Digits) # shuffle the digits
nDigits = len(Digits) # check how many digits
thisExp.addData('new_digits', Digits) # store the shuffled digits to data file
``````

Try

``````digitList = Digits.split(",") # make a list of numbers
shuffle(digitList) # shuffle the digits
nDigits = len(digitList) # check how many digits
thisExp.addData('new_digits', digitList) # store the shuffled digits to data file
``````

Thank you, that prevented the error.

I have managed to adapt your digit-span demo for my purposes now - thanks!! - One thing, I noticed that keyboard input was being recorded prior to the response collection screen (during select_digit). Once the response collection screen appears (trial), any keyboard inputs from the start of the trial (ie., during select_digit) are present on screen. This means that if participants notice, they could easily copy the digits as they are presented, removing the memory demand of the task. I am confused why this is happening, because the code from the “trial” routine tab sems to clear the response on each loop: `captured_string=''` . Do you know how I could alter it to only record keyboard input during the response collection screen?

Overall flow:

code from trial routine:

Off the top of my head, I think you need event.clearEvents() in Begin Routine to clear the keyboard buffer.