| Reference | Downloads | Github

Conditional Branching by Trial and Key Trigger


OS Mac & PC (I am working on this primarily on Mac but plan to also have it run on PC)
I am new to PsychoPy and I have little programming experience so I have several questions. My first question is whether what I am trying to do is even possible in Builder. I am trying to do conditional branching per trial rather than per block and based off of key presses. So, I have a word production task in which participants view a stimulus that is either the definition of a word or a picture of a person with a short description of who they are. There are 100 stimuli and participants can response in one of three ways:

  1. Press Key 1 (‘v’) to indicate that they know the answer. Then, they would see a screen of infinite duration (PracMic) in which they are to say the answer out loud before moving on to the next trial (terminated by ‘space’). (If I can only skip one Routine easily, (you’ll see in the “what I’ve tried is depicted” section below), then skipping only PPList is okay with me).
  2. Press Key 2 (‘b’) to indicate that they do not know the answer. Then, all following Routines should be skipped and participants should simply go to the next trial (in PracTrial).
  3. Press Key 3 (‘n’) to indicate that they are experiencing a tip of the tongue state (they know the answer but just can’t produce it right now despite feeling like it’s on the verge of coming back to them). Then, they would first see a pronunciation task for 30 seconds, see the stimulus again, and if they know it, the PracMic screen, if not, a recognition task (PracRecog).

Here’s a visual of the participant options:

What I’ve tried is depicted here:
I’ve tried adding code in the PracTrials to get it to skip the Routines I need it to, but it keeps playing all the Routines in the Loop. To skip, I’ve been trying ContinueRoutine = False. Is this something I need to just switch to Coder for because it’s too complicated for builder?
If I must do this in Coder, what is the integral code for the key trigger? I don’t think event.getKeys is sufficient. Should I be making a variable like keyTrigger = event.getKeys() and then setting conditional statements like if keyTrigger==‘v’: ?? Should I even set up Routines in Coder like PPList, PracMic, or PracRecog, or should I just have visual.TextStim() of the events based off the conditional statements?

I know this is a lot, I just realized how complicated my experiment is by attempting to make it a reality. Thanks in advance!

P.S. Everything is labeled “Prac” because I have to do everything again for the test trials (100 test, 10 practice). The 10 practice are different stimuli than the 100 test.


1 & 2 are actually quite straightforward in Builder. Insert a code component in the PracMic routine and put this in the "begin routine " tab:

if your_keyboard_component_name.keys.lower() != 'v':
    continueRoutine = False

[Code edited after initial posting to remove an error.]

If the response is anything other than 'v' then the routine won’t run. You don’t need to handle a response of 'b' explicitly, as it will fail this check anyway. We use the lower() function to make responses lower case, to handle if the subject engages the caps lock key.

(3) I’m not sure I really understand as you don’t explicitly match this to the actual routine names other than PracRecog, but hopefully you can see how to insert the same sort of code on those routines, checking for 'n' responses, to get what you want.


Hi Michael,

Thanks for your quick response! I copied and pasted your code and renamed the variable and got an error back.

if resp.keys().lower() != ['v']:     continueRoutine = False

TypeError: ‘str’ object is not callable

What does that mean?

Sorry about being unclear, the PPList Routine is the text stimulus of the pronunciation task depicted in option 3. So for Key #3, I would like the “PPList” Routine to continue (show for 30 seconds) and then the original routine, “PracTrial” to reappear after that finishes. If the participant again presses TOT, then it should go into the “PracRecog” Routine. If instead, at this point, they press Know, then it would go to the PracMic.

Thanks again,


Sorry, we should try dropping the brackets here (sometimes the keyboard component returns a list of keys, sometimes just a single key, depending on the settings):

if resp.keys.lower() != 'v':     
    continueRoutine = False

Sorry, I forgot to reply to this thread. I’ve deleted your new duplicate question so that everything stays in one place.


Hi Michael,

if resp.keys().lower() != 'v':

TypeError: ‘str’ object is not callable

It’s unfortunately still not working and has the same error. I even tried rebuilding in PsychoPy 1.9 with Python 2.


Note the () after .keys() are the ones causing this error. i.e. we need to drop those round brackets as well as the square ones around the 'v'


Oh my goodness, I feel so silly! Thank you so very much!! It’s finally working :slight_smile: