Custom coding to randomize the stimulus with two repeats at most

I’m running a memory experiment in which participants would see sentences first ,and then cues. There are 40 Chinese sentences which corresponds to 2 cues and the cues represents remember and forget respectively. I would like to randomize the sentences but the same cue could appear twice consecutively at most. That means the order " F, F, R, R, F "is ok.


image
image

I have read psychopy coding function tutorial and tried to follow this link : https://discourse.psychopy.org/t/randomisation-without-consecutive-presentations/8089

I adjusted the code several times, and here is the last version:

trialList = data.importConditions('stimu_all.xlsx')
useRows=''
trials=data.TrialHandler(nReps=1,
    method='random',
    extraInfo=expInfo,
    originPath=-1,
    trialList=data.importConditions('stimu_all.xlsx', selection=useRows),
    seed=None,name='trials')
CueWord=''
SentenItem=''
list_ok=False
triple_stim=False
import random
random.shuffle(CueWord)

for thisTrial in trials:
    SentenItem=thisTrial['SentenItem']
    CueWord=thisTrial['CueWord']

while list_ok==False:
    for i in range(len(CueWord)-1):
        if CueWord[i]==CueWord[i+1] and CueWord[i+1]==CueWord[i+2]:
           triple_stim==True
           break 
    if triple_stim==True:
        random.shuffle(CueWord)
        triple_stim=False
    else:
       list_ok=True
print(thisTrial)

The problem is that only one trial appears in the experiment, so I only see one sentence and one cue, and then the experiment move on to the next routine.
Because I know little about coding, I’m not sure where goes wrong. Could anyone help me please?

I think everything from your while loop onwards needs to be indented one more step so that it is included inside the for loop.

1 Like

Thanks for your help. I indented the while loop, but there was still one trial rather than the whole list. So I only saw one sentence and cue.
I indented the while loop like this:

trialList = data.importConditions('stimu_all.xlsx')
useRows=''
trials=data.TrialHandler(nReps=1,
    method='random',
    extraInfo=expInfo,
    originPath=-1,
    trialList=data.importConditions('stimu_all.xlsx', selection=useRows),
    seed=None,name='trials')
CueWord=''
SentenItem=''
list_ok=False
triple_stim=False
import random
random.shuffle(CueWord)

for thisTrial in trials:
    SentenItem=thisTrial['SentenItem']
    CueWord=thisTrial['CueWord']
    while list_ok==False:
        for i in range(len(CueWord)-1):
            if CueWord[i]==CueWord[i+1] and CueWord[i+1]==CueWord[i+2]:
               triple_stim==True
               break 
        if triple_stim==True:
            random.shuffle(CueWord)
            triple_stim=False
        else:
           list_ok=True
print(thisTrial)