| Reference | Downloads | Github

Added code component to break out of loop, but trials continue

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 1.90.1
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?: A break every 48 trials using the modulo operator.

What did you try to make it work?: I added the following code component. I’ve tried adding it to the “Begin Routine” and “Every Frame” tab of both the “trial” routine and the “breakText” routine:

if trials.thisTrialN % 48 == 0:
continueRoutine = False

What specifically went wrong when you tried that?: It never breaks out of the loop. When I added the following line of code, I discovered that it is skipping the 48th trial, but is never breaking out of the loop and showing the breakText:


The flow of my experiment is below. It’s fairly basic, it presents stimuli to participants and just asks them to give an answer using the left or the right key. There are 192 trials in a block, however, so I’m trying to get it to show the breakText “Take a break! Press space to continue” every 48 trials. As stated above though, when I insert the code component it is never breaking out of the loop. (27.0 KB)

I’ve attached the entire code for my experiment above.

Any help at all would be massively appreciated.


@kdundas, you will want to use the loop.finished command, e.g., trials.finished=True - see terminating a loop docs.

@dvbridges Thanks so much for your quick reply!

I’ve now changed the code to:

if trials.thisTrialN % 48 == 0: trials.finished=True

and placed it in the “begin routine” tab and then in the “each frame” tab, and in both cases it’s ending the routine after just one trial.

I would assume this happens because the first trial number is 0, and 0 % 48 is 0, so your conditional statement is true.