psychopy.org | Reference | Downloads | Github

Loop.finished=true No longer working

Hi David,

I’m surprised that this works (or perhaps it doesn’t any more) since I’m pretty sure currentLoop.thisN needs to be changed to trials.thisN

Please can you clarify when you can use the custom loop name and when you have to use trials?

Best wishes,

Wakefield

Hi @wakecarter, online you should always use trials when making reference to loop parameters, such as thisN or finished. This is because the trials argument for each function in the JS code is a snapshot of the current state of the trial handler, e.g., current trial number etc. It was originally set up so that currentLoop should make reference to the trial handler, as in the code you will see currentLoop = <loop name>, but this will not work because you need the snapshot instead. The generated code could be changed in future so that currentLoop points to the trials snapshot for each trial.

1 Like

Ah. I took currentLoop to be an example of a custom loop name rather than something with a specific meaning.

Hi @dvbridges

I know that this topic is heavily discussed in recent months due to PsychoPy versions however none of the solutions worked for me.

PsychoPy version = v2020.2.5

What I want to do: Participants should not perform the cate_name1_last loop at all if they do not make any mistake in the previous loop (cate_name1). If they do mistakes the mistaken trial To do a mistakeless first loop minimum of 20 trials are needed

Simply they see images and they need to choose one of the two buttons to respond. If they make a mistake, the mistaken image is added to an empty list. The list that is created by only mistaken trials need to be shown in loop cate_name1_last however if they do not make any mistakes in the previous loop experiment crushes because the list is empty. So I have the following code in the Routine named rountine_1_end
PY

if len(learning1) == 0:
    cate_name1_last.finished = True
else:
    cate_name1_last.finished = False

JS

if ((learning1.length === 0)) {
    cate_name1_last.finished = true;
} else {
    cate_name1_last.finished = false;
}

However, I get no error message, and although the list learning1 has no item loop is not terminated.
Whenever I use

> trials.finished=true

I get the following error: error

I am a bit confused about what I need to do at this point.

Many thanks

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.

from:

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

to:

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.

image

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.

Phil

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.