Selecting condition files depending on participant number

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 3.1.0
Standard Standalone? (y/n) If not then what?: yes
What are you trying to achieve?:

in a memory experiment, I have two different trial routines, routine A and routine B with the loops trialsA and trialsB. For each loop, I would select different conditions files depending on the participant number, so e.g. for participant 1 the conditions file “memory_0” should be selected, for participant 2 “memory_1” and so on. I present rectangles in different colours. The colour of the rectangles and other stimulus properties are defined in the conditions files.

What did you try to make it work?:
I inserted the following code component at the beginning of routine A which is called “trial_A”:

for thisTrials_A in trials_A:
    if expInfo['participant'] == '1':


    elif expInfo['participant'] == '2':


What specifically went wrong when you tried that?:

I get following error message:
NameError: name ‘recColor’ is not defined

“recColor” defines the color of the rectangle and is specified in the conditions file. So I guess the problem is, that there is no conditions file loaded at all. What do I have to write in the conditions line when defining the loop properties…? Does anyone have any ideas…? Looking at the script, I think the problem is, that all this

trials_A = data.TrialHandler(nReps=1, method='random', 
    extraInfo=expInfo, originPath=-1,
    seed=None, name='trials_A')
thisExp.addLoop(trials_A)  # add the loop to the experiment
thisTrials_A = trials_A.trialList[0]  # so we can initialise stimuli with some values
# abbreviate parameter names if possible (e.g. rgb = thisTrials_A.rgb)
if thisTrials_A != None:
    for paramName in thisTrials_A:
        exec('{} = thisTrials_A[paramName]'.format(paramName))

for thisTrials_Ain trials_A:
    currentLoop = trials_A
    # abbreviate parameter names if possible (e.g. rgb = thisTrials_A.rgb)
    if thisTrials_A != None:
        for paramName in thisTrials_A:
            exec('{} = thisTrials_A[paramName]'.format(paramName))

comes before my code component.
Would be great if someone had a suggestion or an idea what I could try.
If anything is unclear, please let me know!
Thanks a lot in advance!


you can select a condition file based on a participant number right from within the loop properties and without code component.

For example, we use the following input in the conditions textfield of the loop properties to select an appropriate balancing condition based on the participant number:


Just adapt it to your needs.

All the best,


Hi Frank,

thank you so much. That works :grinning:


Hi Frank,
I have tried this in Win10 desktop standalone (v2020.1.3)
but the loop/trial properties won’t accept as it cannot find the variable files. The error state ‘No parameters set (conditionsFile not found)’.

I have even removed the ’ ’ in case these were causing the complications.
what I typed in:

as the files are called ‘participant number’_encPrep.csv

Any idea what could be going wrong?

Please could you show exactly what you tried (in the $'playlists/balancierung_'+str(int(expInfo['participant'])%4)+'.xlsx' format) and the name and location of an example csv file?

Given your example, I assume that it should be as follows:

This assumes that your csv files are located within the working directory of your experiment.

It’s important to avoid “intelligent” quotation marks.

1 Like

This worked!

Thank you very much for the swift reply.

Hello I’m very new to PsychoPy, and I’m trying to counterbalance my online experiment, and I have set up my loop in the psychopy Builder as:


But when I upload to Pavlovia, it seems to crash–which I’m assuming is happening because the input in builder does not translate to javascript like the rest of the python code does. So basically I’m just asking to make sure that if I translate the input to javascript it should work the same, or is this something I am not able to do with my online experiment?

Any help is greatly appreciated.

Only code components get translated to JavaScript, so if you want custom code to appear in a field like this, you should calculate a variable in a code component and then put that variable name in the loop dialog field. That means it should work in both Python and JavaScript.

e.g. something like:

conditions_file = str(int(expInfo['participant']) % 4) + '.csv'

and then put this in the loop conditions file field:


If the auto-translation of the code to JavaScript is valid, then this should work whether locally or online.

1 Like

Thanks for the reply, it works great! Thanks again.