End loop does not work

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

OS
PsychoPy version 3.2.4

What are you trying to achieve?:
I am trying to end a loop under a certain condition

What did you try to make it work?:
I used the following if statements (note that time and scotest are defined in the same trial (trial_2) but a different routine; I putted this routine with code at the end of the trial_2:

if time ==1 and scotest ==1:
continueRoutine = False
fine.finished = True

elif time ==1 and scotest ==0:
continueRoutine = False
trials_2.finished = True

What specifically went wrong when you tried that?:
I checked the output and time and scotest are given correctly. However the trial does not end when I want… I checked the forum and found multiple problems showing the same problem (only mostly when using the online version of Psychopy which I dont use) but I cant figure out how to solve this or what goes wrong…

Hi,

Without a bit more context I can’t be 100% sure, but it looks like you might need to finish your inner loop too in the first if statement?
i.e.

if time ==1 and scotest==1:
    continueRoutine = False
    trials_2.finished = True
    fini.finished = True

Thanks for thinking along!
Ah this changes something… but now instead of doing nothing (so it improved haha), it only skips the ‘inner’ trail (trials_2)… The reason I added this outer trial (fini) is because I want participants with a certain score (scotest ==1) and after a certain time (time ==1) to end the experiment and skip a final third trial sequence (that is inside the fini loop, set up after trials_2). In the first picture you see this third trial part and that this ‘fini’ trial indeed ends before the end screen of the experiment. The code however does not seem to respond to ending this outer fini trial… and now continues to trials_3 anyway…

Maybe I did something wrong in setting up the fini trial? See picture 2…

Hope I explained it well!

Hi there,

OK so it sounds like you want to also skip all subsequent routines once a score has been obtained, you want to add a small bit of code to the start of the routines that could be skipped.

For example:
In the ‘begin experiment tab’
endTask = 0

then in your ‘begin routine tab’ (where you already have this):

if time ==1 and scotest==1:
    continueRoutine = False
    trials_2.finished = True
    fini.finished = True
    endTask = 1

then in subsequent routines (that could be skipped in theory) in the ‘begin routine’ tab:

if endTask:
    continueRoutine = False 
    fini.finished = True

Becca

Thanks! Yes that is what I want! However, I want to skip routines in another loop (trials_3) and a separate text block (instruction_3).

The code I have is in the routine ‘ghh’ part of trials_2. But how do I skip instruction_3 and trials_3? That’s why I thought, let’s make a loop (fini) that covers all trials and can be ended and lead the participant to the ‘end’ routine (i.e. skip instruction_3 and trilas_3) in case time and scotest both are 1…

Hi OK,

If you want to skip a whole bunch of routines contained in a loop you can set nReps.

Where you have this bit of code:

if endTask:
    continueRoutine = False 
    fini.finished = True
    trials_3_reps = 0

then in your ‘trials_3’ set nReps to be ‘trials_3_reps’

Becca

1 Like

Yes it works, that’s really fantastic, many thanks!!

no worries! please can you mark the solution for future users?

Thanks,
Becca