| Reference | Downloads | Github

Conditional Branching for Picture Naming Trials



The PsychoPy version I am using is v3.0.0b12 standalone on Windows 7 Professional.

The barebones setup for the study I am trying to set up is as follow:

(1) Show participant a picture (e.g., ‘chocolate’) for naming
(2) If participant is able to name (e.g., ‘chocolate’): Proceed to next picture trial (e.g., ‘chair’) for naming
(3) If participant is unable to name (e.g., ‘chocolate’): Provide prompts (e.g., audio + written) to help naming that picture (‘chocolate’), then proceeding to next naming trial irrespective of whether naming was correct or not.

The experimenter will be at the side controlling button presses for steps (2) and (3) to indicate to the program whether participant had named correctly or not, so there is nil need for voice recognition.

So, far this is what I have done:

For code2, this is what I have typed:

  • Under ‘Begin Routine’ tab:
    stimuli = [image_prac]

  • Under ‘Each Frame’ tab:

    if key_resp_prac== corrAns: # correct response
            continueRoutine = False
            trials.finished = True
    else: # incorrect response
            continueRoutine = False
    break # stop checking after the first detected stimulus (in case of overlaps)

What specifically went wrong when you tried that?:
The problem is that the entire program runs and immediately ends, without displaying anything. There are no error messages either.

I am completely new to PsychoPy and programming. Please assist. Thanks!



Try thinking like a computer. They only do exactly what you tell them to do.

On your code that runs on every frame (typically 60 times per second), you check if the response is correct and then do something. This is where you fall at the first hurdle: if key_resp_prac== corrAns: doesn’t check if the response was corrAns: it checks whether your keyboard object was corrAns, which is always going to be untrue. With computer code, you must very precisely ask the right question. By analogy, in the real world, it doesn’t make sense to ask "is my computer keyboard the letter n"? So the right question to ask here is something like:

if corrAns in key_resp_prac.keys:

i.e. you’re not asking if the keyboard is a letter, you’re asking if the store of keypresses attached to the keyboard contains the letter you’re interested in.

But also, the else clause doesn’t imply that the subject made an incorrect response, it only implies that absolutely anything else happened, including making no response. So what could happen is that the subject does absolutely nothing, which you are checking for 60 times a second and ending the trial accordingly. So with a study designed to last 20 trials, say, the entire experiment would finish in one third of a second. There would be no errors, because according to your code, absolutely nothing went wrong. The experiment completed as described.


Thank you, Michael, for explaining.

I have adjusted the codes.

Under ‘Begin Routine’ tab, it now reads:

stimuli = [image_prac]

for stimulus in stimuli:
    if corrAns in key_resp_prac.keys: # correct response
            continueRoutine = False
            trials.finished = True
    elif corrAns not in key_resp_prac.keys: # incorrect response
            continueRoutine = True
    break # stop checking after the first detected stimulus (in case of overlaps)

The program runs but not in the intended manner. Instead of showing the picture without prompts (e.g., audio + written) first, the prompts are now shown together with the picture right from the start in the first trial. This continues regardless of whether the response is correct or incorrect.

I suspect my ‘if’ and ‘elif’ codings are still incorrect. Please assist. Thanks!


OK, I’ve gone back to your first post and think we need to restructure things quite a bit, which should make things work much more easily for you.

I think you should split your trial into two routines. On the first routine, put the fixation cross, the image, and the initial keyboard response.

On the next routine, put the audio and text prompt and the second keyboard component. Each component is set to “Force end of routine”.

Now all you need to do is stop the second routine appearing if the response on the first one was correct. Simply insert a code component on the second routine and put this in the “begin routine” tab:

if key_resp_prac.corr: # if correct response on prev routine, 
    continueRoutine = False # don't show this one

As far as I can see, you just need one loop (connected to your conditions file).


Dear Michael,

Thank you so much. It now works.
Apologies though if the queries were elementary, but huge thanks!