Solved: Error in terminating loops

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

OS (e.g. MAC OS 10.13):
PsychoPy version (e.g. 1.85.4):
Standard Standalone? (y)
What are you trying to achieve?:

I would like to terminate a loop before it activates the last routine for the last cycle. Please see the flowchart below:

The look named block repeats 3 times, but at the last cycle, I want to terminate the block loop before it runs the break_2 routine.

What did you try to make it work?:

In the builder view, I added the following code to the routine break_2:

begin experiment:
blockCount = 0

begin routine
blockCount = blockCount +1
if blockCount =3
block.finished = True

What specifically went wrong when you tried that?:

I couldn’t run the program because it shows an error “invalid syntax” pointing towards if blockCount = 3

I would like to know how can I modify this to get it work. Thank you in advanced!

In Python, we check for equality using ==. A single = is for assigning values rather than comparing them.

The solution is simpler than you are trying to implement. You don’t actually want to end the loop prematurely: you just want the final routine to not run when that loop is on its final iteration. You also don’t need to keep track of counters, as the loops do that for you automatically. So you only need two lines of code (in the “begin routine” tab):

if block.thisN == 2 # on the third iteration:
    continueRoutine = False # don't even start this routine

Hi Michael,

Thank you so much! This is very helpful, and the issue has been solved!