Using code to create conditional attention check routine

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Mac Catalina 10.15.7
PsychoPy version (e.g. 1.84.x): 2021.2.3
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?:
In my experiment, I have 56(randomized) trials. Each trial consists of an inner loop with 6 randomized priming stimuli, then a test stimuli.

My PI would like to include attention checks every 20 trials (out of the 56) with a conditional routine. He suggested using the code if mod(trialNum,19)==0 to achieve this. Picture of my flow is uploaded.

What did you try to make it work?:

My first thought was to create a loop just for this routine after the test stimuli, and set the loop condition to if mod(trialNum,19)==0. This definitely didn’t work. I tried this code in the nReps box of the loop as well and that did not work. I am wondering if I need to stick this code in a code component of the routine and forget the loop? I am not familiar enough with coding to know exactly what syntax to use. My hunch is I need something in the begin routine portion of the code that says if mod(trialNum,19)==0, then …

What specifically went wrong when you tried that?:
invalid syntax on what I have tried so far

I think a solution is to have a code component in the conditional routine that skips the routine (with continueRoutine = False) unless on specific trials number. You can also set a manual counter that goes up by 1 at the end of each trial routine.

do you have advice for how to phrase this?

I think it should be something coded in the beginRoutine section, something like


if trialNum = (an array of my routine numbers would go here, but I am not sure how to use syntax properly for it??)
            continueRoutine = False
elif trialNum = 21, 41
          continueRoutine = True

Try something like

if trialLoop.thisN != 21 and trialLoop.thisN !=41
          continueRoutine = False 

Thanks! It didn’t crash, so that’s a start. However, it didn’t play the attention check at all. I am guessing I need some sort of begin experiment code component where it prompts it to keep track of which rep of trialLoop it is at? would it be something like trialLoop.thisN = 0?
I tried moving the code to a different tab - Begin/Before Experiment instead of Begin ROutine, and it crashed (undefined term “trialLoop”) so it seems it is in the right place but missing something

My code above is missing a colon after 41.

.thisN is automatically assigned to loops. The first loop has a value of 0.

Unfortunately that is not the error, as I added the colon when I put this code in. It’s currently in “each frame” but I tried “begin routine” and it still never shows the routine

It should be in Begin Routine. What routine is it in? I was only guessing about which loop counts up to 41.

This helped! I figured it out - my outermost loop functions as the randomziation tool, then I have the trial loop which runs the routines, and the trainingLoop which has priming stimuli that are randomized and specific to each trialLoop repetition. Moving the attention check from the inside of trialLoop (as I had it originally) to the randomizer loop and changing the code to use randomizer.thisN worked!!

1 Like