Random routines

OS: Win10
PsychoPy version: 3.1.2

I am trying to construct an experiment in which there will be 20 base rate neglect tasks, each following the memory task and metacognition assessment + 3 practice tasks. In short, each task is a routine. I am wondering, is there a way to make routines random for each participant? And if so, I would like to have tasks following each other not randomized - in other words, there are ‘‘task pairs’’ (actually, there are 3 tasks - base rate, memory task and metacognition assesment) which I would like to treat as one task, while randomizing.

I am sorry if I am not being clear. Thank you in advance.


This sounds unwieldy. If at all possible, you should aim to reduce this amount of repetition, it is usually a sign of an inefficiently-coded experiment. i.e. are the 20 tasks actually completely different? Or do they share a similar structure, and could be presented with a smaller number of routines, where stimuli and other conditions are controlled in a way that allows the 20 tasks to be expressed?

Yes. You can insert a code component on each routine (from the “custom” component tab in Builder), and in its “Begin routine” tab, insert something like this:

if not some_condition:
    continueRoutine = False

i.e. before each routine even starts, you can run a check as to whether it should immediately terminate and go on to the next one. We would need to know more about the logic of your experiment to suggest the exact condition to use. But, for example, you might put the name of the task to run on each trial in a column in your conditions file (called, say, task_name), and then check that:

if task_name != 'the_name_of_this_routine':
    continueRoutine = False

Thanks for your answer.

I am not sure if I can use less routines - 20 tasks have similar structure but they are different substantially (participants are presented with 20 different base rates - for example: “Person A is funny. There are 790 accountants and 210 clowns in the study. Is person A more likely to be an accountant or a clown?”).

Yes, that sounds exactly like it should be one routine. Instead of specifying fixed values for the text stimulus, you simply control the values that are presented on a trial-by-trial basis, either using variables in a conditions file (most likely, and easiest), or via some code snippets.

This not only simplifies the creation and maintenance of your experiment, but will dramatically ease things at the data analysis stage, as your data output file will be much better structured.

Come back to us with more details of your task if you need more assistance with implementing it. But for a start, your conditions file could look something like this:

attribute  first_n  first_profession  second_n  second_profession
funny      790      accountants       210       clowns
boring     210      accountants       790       clowns
asinine    100      economists        600       bloggers
# etc etc

Then in your text stimulus, you could put in an expression like this in the text field, set to update on every repeat:

$f'Person A is {attribute}. There are {first_n} {first_profession} and {second_n} {second_profession} in the study. Is person A more likely to be an {first_profession[:-1]} or a {second_profession[:-1]} ?'

i.e. you mix your fixed sentence elements with variables from the condition file attached to the loop controlling your trials.

In this case, we automatically trim the “s” from the professions on their second occurrence, but this might be a bit crude.

These formatted “f strings” are a feature of Python 3, so you need to be using the Python 3 version of PsychoPy to use this. Otherwise, they need to be formatted using Python 2 syntax.


Hi Michael,
The task you have already helped me with is a part of an experiment which involves remembering a picture before solving a task, after each task there should be an assessment of rightness of answer, and then the participant has to choose which of the pictures is the picture shown before the task.

I have two questions:

How do I achieve that random picture shows before each task, then that the random task is given, and then that participant has to assess rightness of answer (for each individual task), and then chooses between different pictures, randomly selected, including the right answer? I am not sure how to make this order of events repeat for 20 tasks that I have.

When I uploaded pictures, there was a ‘‘PIXI representation of stimulus unavailable’’ shown to my participants online. How do I upload pictures on imagestim and what path should I put in order to avoid this error?

All details in a row in your conditions file remain current for the duration of a trial. So if you split your trial into several consecutive routines, the variables are still available. So you might have a column called, say image_name, with value like dog01.jpg, and another column called correct_answer, which on the same row might contain a value like dog to check against the typed response.

Hi Michael, I’m preparing a similar experiment where I have three inner loop with three routines (as you can see from the first screenshot). The first routine has an excel file with one video only to play, the second one has an excel file with three video and I want to play just one randomly from it (to do that I used a code component and in the “experiment begin” section I wrote “from numpy.random import choice” and in the loop section I wrote what you can see from the second screenshot. Finally in the last one I have a video component with a video set as constant and a sound component for which I randomly select 4 sounds from an excel file column composed by 10 sounds (to do that I used the same “choice procedure” as the second routine).

My goal is to create an outer loop able to randomly play these different inner loops for each participant but I don’t know how to do it. I also tried to look here Blocks of trials and counterbalancing — PsychoPy v2021.2 but it didn’t work.

It would be great if you could help me.


SOT_4_LRC_stimulus.xlsx (8.5 KB) SOT4REVERSE_stimuli.xlsx (8.5 KB) sounds_4_routine.xlsx (8.6 KB)

Hi, please define precisely what you mean by this.

1 Like

Inner loops: “trials”, “trials_2_reverse” and “trials_3”. My goal is to make them randomized for each participant.

Thank you again

Because now if I run the experiment, the order of presentation of the loops is always the same (trial, new_routine, new_routine_2) , and I would like this to change for each participant.

OK, you need to insert a fourth loop, that surrounds all three of the existing loops. This outer loop will run three times, but on each iteration, only one of the inner loops will be allowed to run.


  • Insert that fourth loop, set its nReps value to 3, and don’t connect it to a conditions file.
  • In the trial routine, insert a code component (from the “custom” component panel).
  • In the “Begin experiment” tab of that component, insert the following code, which will run once at the start of the experiment to randomise the order to be used for this session:
# specify which of the three inner loops will run on each of
# the three iterations of the outer loop:
orders = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

# randomise that order for this session:

Now in each of the inner loops, you will want to replace the current fixed value of 1 for nReps to be a code expression which will evaluate to either 1 (the loop will run on this iteration) or 0 (this loop will not run):

  • trials loop:

  • trials_2_reverse loop:

  • trials_3 loop:


i.e. orders is a list that contains 3 sub-lists, each with three entries. The trials loop will always look up the zeroth list within orders, and then look up the 1 or 0 value corresponding to the current iteration of the outer loop, and so on for the other two loops, so that only one loop runs on each iteration of the outer loop. This is why we use two indices, e.g. orders[0][2] is entry number 2 of the zeroth list within orders.