| Reference | Downloads | Github

Switch between routines after varying numbers of trials of each

What are you trying to achieve?:
Hi there. I am attempting to build an experiment involving two different tasks (each assigned to a separate routine). The tasks are switched back and forth, after varying numbers of trials.

Every subject will follow the same predetermined sequence of trials, except for some differences described below…

However, the number of task one trials that subjects will complete before the experiment switches to task two changes over the course of the experiment (same for all subjects). For instance, initially subjects perform 10 task one trials before task two appears, but then then the next time around they perform 25 task one trials before task two starts and so on.

Task two trials will repeat until the subject makes a mistake.

I am very new to PsychoPy, but willing and have time to learn. Some pointers from experts as to the right way to start this would be very helpful as it feels like a complicated design for a first try at programming an experiment and I have had trouble thus far.


Hi Dan,

This sort of thing can be done in Builder by inserting a little custom code in a code component. But the code needs to be very specific to your particular design. At a guess, I think you will need a three-loop design, one around each task’s routine, and a third, outer loop that encompasses both.

The two inner loops probably need to be connected to their own, different conditions files. The outer loop is connected to a very simple file that just specifies the number of trials for the first task on each sequence.

eg create this conditions file for the outer loop:


insert a code component on the task one routine and put this in the “start experiment” tab:

task_one_start_row = 0

In the loop dialog, put something like this in the “selected rows” field:

task_one_start_row:(task_one_start_row + task_one_repeats)

We need to update the start row for the next iteration of the outer loop. Do this on the first (ie zeroth) iteration of the task one loop, with code in the “begin routine” tab:

if your_loop_name.thisN == 0:
    task_one_start_row = task_one_start_row + task_one_repeats

That should answer the first part of your question, although will need careful testing, especially with checking the data file to make sure that there are no off-by-one errors and that no rows are skipped or repeated. If you are randomising the order of the rows in the loop, you’ll probably need to unspecified a seed value in the loop dialog so that the same random order is used on each iteration of the outer loop. If you want the order to vary across subjects, the you’ll need to set a different seed value on each session (there are posts here about how to do that.