| Reference | Downloads | Github

Call specific routine depending on trial type


For each trial of this experiment, participants will first hear an audio sentence, then receive 1 of 3 cues randomly (“Non-word”: say the non-word displayed out loud, “Picture”: decide if the displayed picture is a living object? Yes/No decision, or “Nothing”: blank screen) which will indicate the type of task they need to do for a delay period of 14 seconds before recalling the sentence out loud. There will be 99 sentences altogether, to be randomly paired with 1 of the 3 cues (33 sentences for each cue – I’ve coded this component based on a previous answer, Randomize sentence with one of three cue types without repeating). I have built all the different tasks in PsychoPy, each as individual routines. But I am having troubles figuring out how to call the specific routine depending on the cue-type. For example, if the trial is a non-word task, the trial should be: 1) audio --> 2) Non-word [the screen will explicitly tell them which task it is] --> 3) “FLIMP” (then they will repeat ‘flimp’ every time the fixation cross appears), or if the trial is a picture task: audio --> Picture --> [picture] with response key activated (repeated with several pictures).

I have tried to modify this code (Switch between routines after varying numbers of trials of each) but cannot seem to figure out how to make it randomly switch, instead of e.g., 10 trials of non-word, then 10 trials of picture task etc. Any help will be appreciated. Thanks!


As per the first thread you link to, you need to describe the randomisation specified in your design. Do you accept a random cue type selected for each trial, or should the randomisation be constrained by counterbalancing to have 33 of each? Does the randomisation vary across subjects, and so on.

Please also don’t say “for example…” when describing what routines should run for each cue type. To suggest what to do, we need an exact description of each option.

Please also provide a screen shot of your flow panel so we have a common framework to refer to.


It’s a within subject design, 33 sentences will randomly be assigned to each of the three cue types. Sentences do not repeat. Using the code in the first link, I have a single conditions file containing all 99 sentences and their three columns of associated cues. In the experiment it looks like this: a sentence is presented followed by one of the three cues.

If the cue is “Non-word”, then in the next task, the word “FLIMP” will appear on the screen and participants will repeat ‘flimp’ each time a fixation cross appears.
If the cue is “Nothing”, then in the next task, the screen is blank for 14 seconds
If the cue is “Picture”, then in the next task, they will make yes or no decisions about the pictures.
I have each of these as a routine within a trial, but only one of the task needs to be called depending on what cue was presented after the sentence.

I think I would need to add some code specifying what the next task will be in the “sentence_cue” routine because this is where the cue is specified. Probably also insert a loop around the three task so that only one block is ran depending on the cue. This is where I am having troubles figuring out what to code and where

I hope this clarifies the question. Thanks in advanced!!


OK. Insert a code component in the sentence_cue routine. In the “begin experiment” tab, put something like this:

# list of 99 cue types:
cue_types = ['Non-word', 'Nothing', 'Picture'] * 33

# randomised per subject:

In the “begin routine” tab, put something like this:

# extract a cue type for this trial:
current_cue_type = cue_types.pop()

# record it in the data file:
thisExp.addData('cue_type', current_cue_type)

Non-word routine code component “begin routine” tab:

if current_cue_type != 'Non-word':
    continueRoutine = False
    trials.finished = True

Nothing routine code component “begin routine” tab:

if current_cue_type != 'Nothing':
    continueRoutine = False

Picture routine code component “begin routine” tab:

if current_cue_type != 'Picture':
    continueRoutine = False
    trials_2.finished = True

But I’d recommend re-naming the two inner loops to be more descriptive. It will help with interpreting the data output, because those loops don’t correspond to trials. You may also want to consider whether the “is trials” checkbox should be selected in those loops. No if you’re not collecting data on them, probably yes if you are. You need to check what impact they will have on your data file output structure. If checked as “is trials”, you will end up with multiple data rows per iteration of the outer trial loop.


Thank you!!


Hi Michael,

Sorry, I hope you don’t mind if I ask a follow-up question. I just realized that I will also need to take into account the sentence type (long and short). With the current code, the conditions are sometimes balanced and sometimes not for each participant. I’ve reduced it to 96 sentences so that each participant can get 16 trials of each condition (long sentence/non-word cue, long/nothing, long/picture, short/non-word, short/nothing, short/picture). How can I amend the code to do this? Thanks again for all your help!


OK, so you would need to create two separate lists, one for each sentence length. We will store those lists in a dictionary, so each can be accessed by name to extract a cue type from the relevant list as required:

cue_lists = {} # an empty dictionary

for label in ['Short', 'Long']:
    # create a list of 48 cues for that sentence length:
    cue_lists[label] = ['Non-word', 'Nothing', 'Picture'] * 16
    # randomise each separately:

You now have a dictionary called cue_lists, and you can access each of the two lists in it by name.

So replace the line for extracting the current cue type with the line below. (I’m assuming you have a variable called sentence_length containing values of 'Short' or 'Long' in your conditions file. Adjust as required.)

# extract a cue type for this trial:
current_cue_type = cue_lists[sentence_length].pop()


Thank you!!!