Problem with pausing a loop - missing trials/stimuli

OS (e.g. Win10): Win 7 and 11
PsychoPy version (e.g. 1.84.x): 2022
Standard Standalone? (y/n) y

Hi in my experiment I need the option to pause a loop at any moment (mostly to check whether participants pay attention to the stimuli and give them a short rest when needed).

I added a short code component:
pauseKey = event.getKeys()
if ‘p’ in pauseKey:
continueRoutine = False
while not event.getKeys(keyList=[‘r’]):
continueRoutine = True
if ‘escape’ in pauseKey:
core.quit()

It allows me to pause my experiment after chosen trial, however, I have just noticed that it causes some stimuli didn’t show up (mostly one stimulus after each pause).

Is there any other option to pause a loop without losing trials?

This demo might help! I tend to use a nested loop to repeat the trial if the participant paused the experiment so we don’t loose that trial.

pause.psyexp (15.7 KB)

Thank you. Thanks to your file, I was able to find a bug in my experiment - I placed my code in a wrong place (it should be placed in the “end routine” instead of beginning…)