Controlling loops with conditions and time

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

OS (e.g. Win10): MacOS Big Sur
PsychoPy version (e.g. 1.84.x): 2020.2.10

What are you trying to achieve?: I have two routines within a loop. The first routine has the participant type freely. If they type a certain number of verbs and press enter, then the next routine will show, which contains a reinforcing image and sound. Right now, I have the nReps of the loop set to nextRoutineNreps so that the second routine containing the reinforcement appears only if they have met the criteria. However, the loop simply ends after showing the reinforcement. I want the loop to continue for 10 minutes, always showing the first routine, and only showing the second routine if they meet the criteria.

What did you try to make it work?: I have tried using a code component in the first routine. In the begin routine tab I have:

if trials.thisN == 0: # only on the first iteration,
loop_timer = core.Clock() # start a new timer

in the each frame tab, I have:

if loop_timer.getTime() >= 20.0:
continueRoutine = False
trials.finished = True

What specifically went wrong when you tried that?: The loop still ends after the first iteration if the first iteration is successful in reaching the desired criteria.


what was nReps? You have to set nRep high enough? BTW:

sets the timer to end after 20 seconds. I guess that is for testing only.

Please add triple ` to your code. Then it is properly formatted and formatting errors can be detected.

if loop_timer.getTime() >= 20.0:
    continueRoutine = False
    trials.finished = True

Best wishes Jens

Hi Jens,

I followed the code provided by Michael here: and it was successful in accomplishing what I wanted to do. However, now when the set time for my first loop runs out, my experiment ends instead of continuing on to the next loop. Any suggestions? I’ve attached a screenshot of the experiment.


the following PsychoPy-Exp contains a routine that presents a count-down timer for 5:00 minutes. Notice that I have set the number of repetitions to 1.000 (5 min = 300 sec). So, the routine will “always” loop until 5 minutes are reached.

TimedLoop.psyexp (7.6 KB)

No, from look at your flow view I can decipher what went wrong.

BTW: When the routine “long_words” and the routine “short_words” differ only the length of the words presented I would try to use only one routine. If you decide to change something in the presentation it affects both, short and long words.

Best wishes Jens

What code are you using to end your first loop. It looks like it’s still active when entering the second loop. My best guess would be that the rule that ends the first loop is still true when starting the second.

I believe that the following code is ending the loop:

if timeRemaining <= 0.0:
    continueRoutine = False # end this trial immediately
    trials.finished = True # and terminate the loop (use its actual name)
    countdownStarted = False # only necessary if you'll be using the countdown again elsewhere

Do I need to add an additional countdownStarted = False statement in the end routine tab? I was not having this issue prior to adding the code for the countdown timer.

I have attached screenshots of my code and my experiment.
length_words_conditionTESTCODECONVERT.psyexp (51.3 KB)


once you set countdownStarted to True, it remains True. Set to False in the End Routine tab.

Best wishes Jens

When I do that, it causes the timer to restart every time it loops.

Put a routine in between the two loops to do this reset or use different variable names in the second loop.