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: https://drive.google.com/open?id=1SPYCEwHLhVF1cuYDDA4sqO5kj6mdxkdmzLcBdjtKqK0

What Iā€™ve tried is depicted here: https://drive.google.com/open?id=1lYR79DW0wNFiwnFyrSfJrrULUD9NWFPI
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,
Patricia

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:

1 Like