Loop.finished=true No longer working

I tried the same solution and got the same error. Any suggestions would be greatly appreciated.
changed JS code only. My loop is called trialsloop.


if ((key_resp_YorN.keys === “n”)) {
trialsloop.finished = true;
if ((key_resp_YorN.keys === “N”)) {
trialsloop.finished = true;


if ((key_resp_YorN.keys === “n”)) {
trials.finished = true;
if ((key_resp_YorN.keys === “N”)) {
trials.finished = true;

Hi @philBartlett,

Try this instead:
snapshot.finished = true;

Best, Thomas

Thanks Thomas. Gave it a try and got the following.


Could you try it again but generating code for the most recent version of PsychoPy?

Hi Thomas,

Do you want me to retry using snapshot and then provide you with the javascript code that is generated? I am not sure what you are asking for.


You are using 2020.2.5 which is before snapshot was added. You could update to the latest version, then try it.

I am using 2020.2.10. I just tried it again and got the same error. Is there something I need to do to declare snapshot?
The code I have written is in the “end routine” tab, should it be “begin routine”?

I haven’t tried snapshot myself yet. Try deactivating the study and then reactivating it.

I always change it from piloting to inactive then back to piloting every time I make a change. I just tried it again and got the same error.

I have put in a work around. Not very elegant but it does the job!
I initialize a variable practiceagain = 1 before the loop
If the exit loop condition is met I set a practiceagain = 0.
I put an inner loop around all the routines and set nReps to practiceagain.
That way it goes around the outer loop the remaining of the nReps but does nothing if practice is complete.

1 Like

Thank you all for the very helpful information! I had some issues with this as well (2020.1.3), when I try to run the experiment locally: I am trying to use loopName.finished in a separate routine in the beginning of a loop to determine if the rest of the loop should be showed or not (e.g. for debug purpose, in ‘End Routine’ tab: practiceRepeat.finished = True), and it doesn’t seem to exit the loop before the rest of the routines are showed. The only time a ‘.finished’ line works in the experiment is when it’s the last routine before the end of the loop.
I am likely missing something, but I was wondering if this function only works if there is no other routine after it? Thank you!

Hey @ade_mh, just checking: you’re referring to a offline experiment right?

Hello! Yes, it is offline at the moment, with the plan to use it online in the future.

I’m not sure whether it works if you set it in any routine that isn’t the last one, but I’ve got two pointers regardless.

  1. You could check by printing snapshot to the console in later routines to see if finished is set to true again. See this tutorial for how to print to the console
  2. If so, a workaround could look something like this:
    a. set some other variable to true (finishLoopIteration or something).
    b. Each next routine, at beginRoutine, check if finishLoopIteration true, if so set continueRoutine to false (so these routines get skipped).
    c. At the final routine in the loop, set snapshot.finished to true if finishLoopIteration is true

I am using PsychoPy version 2020.2.10 on Mac OS Mojave 10.14.6. I have a task where a loop needs to terminate after a certain number of trials. I have been using trials.finished = True, and I have never been able to get it to work locally.

I looked at this solution: Loop.finished=true No longer working - #15 by TomH and as part of the troubleshooting, I tried setting trials.finished = True without any “if” statements to force it to end at the begin routine and also at the end routine. I also tried with the name of my routine. Neither worked, and the error message I got was “NameError: name ‘trials’ is not defined,” or similarly, “NameError: name ‘myloopname’ is not defined.”

I tried this method Builder - terminating a loop — PsychoPy v2020.2 where I set up a counting variable and used if statements, and I tried just forcing it to end by setting trials.finished = True.

It appears to me trials.finished is not working. Am I misunderstanding something? Is there another way to end loops?

Yes - the error indicates that you are either trying to refer to a loop before it is created, or using the wrong name for it.

Check the name, and when the code is set to run. eg if it is in a “begin experiment” tab, the loop won’t exist at that point.

That you are trying in one case to refer to a loop called myloopname is suspicious - that is just the sort of thing someone would put in some example code with the clear intention that it is just a placeholder that would be replaced with the actual name of a loop.

Hi, thanks for the response. I put the real loop name in my experiment, I just put myloopname as a filler for explaining the circumstances. I double-checked the names of the loops.

And yet the error message is quite unambiguous - it’s not a PsychoPy-specific thing, but a fundamental Python error of trying to refer to an object that doesn’t (yet) exist. So you need to show us exactly what you are actually doing - show us the custom code you are using, including where and when it runs, along with screenshots of your flow panel.

thanks so much for this solution

however, what happens if i need to exit out of two loops simultaneously (or one of two)?

If you use a current version of PsychoPy you should be able to name the loops you want to exit.