| Reference | Downloads | Github

Breaking simple non trial loop conditionally online

Hi xai,

I’m not sure if I completely understand your question, it would be helpful if you could post your code and a screenshot of your experimental loops.

Maybe just check my reply above, but ‘trials.finished = true’ will quit you out of a loop online. I’m not sure what ‘trials’ refers to, I think it’s a placeholder for ‘repetitions of the loop’ and the trials.finished = true command should break you out of any loop online. for Python (so offline), you should use the name of the loop you want to quit out of, e.g. my outer loop was called ‘Practice_rounds’ but if your loop is called ‘training_loop’ then for Python you would break out of it by saying ‘training_loop.finished = True’

thanks for your response.

it means i have to put some maximum number in outer loop nResp

then i would quit as required criteria matched.

these are some screenshot hope it would be helpful to understand my problem.
code is not working, loop is working for one time only.

prac_block is outer loop, i have given rept variable in nResp section.

Thanks for you response.

Hi zai,

I don’t think your screenshots loaded before you sent the message, as I can’t see them. It might also be easier to actually send both your Python and JS code as text.

Could you maybe just rephrase what your actual problem is and what you are trying to do?

If you want to quit an outer loop based on a criteria in some way, it’s easiest to put a placeholder value at number of repetitions for the (outer) loop. For example, say nRep = 10, and then try to conditionally quit earlier, as in my previous example as well. Why do you need to give a variable in nResp? I am not entirely sure of what you want to do.

added screenshots now…

i am adding this code in “acc” routine in js only to check the desired condition then repeat or not…

this time in outer loop i kept 10 in nResp

if (omm >= 4)
msg = “Let’s try that one more time! Press SPACE key to practice again”;
trials.finished = false;

if (comm >= 6){

msg = 'Well done.. Now ready for main experiment';

after this code,

now it is generating .js file… after running online
it is freezing at initializing the experiment…

Thank You

trials.finished=false; doesn’t do anything.

You have single quotes in one msg statement and double in the other.

Using the auto translate function helps you to avoid simple errors.

Hi zai,

having trials.finished = false and single quotes for one string and double quotes for another shouldn’t be causing any issues.

However, the loop you printed below wouldn’t run. if statement two is true comm >= 6 then statement 1 would also be true comm >= 4.

So first your conditions aren’t right. you should maybe instead do something like, if comm < 6 and else if comm >= 6.

Question1: does your code does what you want it do to offline?

Question2: where do you define ‘comm’? During your trials, you need to define a correct answer in some way. It might be better to use a trial file with correct answers, and then if key_resp.corr == 1 to say ‘comm = comm + 1’ for example.

I would recommend for you to start a new experiment, and just try to only get the practice loop working, offline and online. You could just use this approach:

for the outer ‘practice_rounds’ loop:

  • looptype = sequential
  • untick ‘Is trials’
  • nReps = 3
  • everything else left blank, it should say ‘no parameters set’

then for your inner loop:

  • I have looptype as random (think you should be able to use other types, but maybe use this to start)
  • ‘Is trials’ is ticked
  • nReps = 1
  • conditions = a .csv file with your conditions and corrAns (which you need to break the loop, if you want to base this on correct answers). It should say you have x conditions with x parameters.

This is what my loop looks like (it’s big so the beginning is cut off):

So in the last box inside the inner pracLoop (called Ptrials_feedback), I have a code segment where I keep track of correct answers from the trials. In this code segment, in the ‘begin experiment’ part, for both the Python and the JS code I have PCorrs = 0

Then in the ‘begin Routine’ code segment I have:

for Python:

if key_resp.corr == 1:
feedbackCol = [0,1,0]
PCorrs = PCorrs + 1

For JS:

if (key_resp.corr == 1) {
PCorrs = PCorrs + 1;

Then in the outer loop, the single green box titled ‘BreakPLoop’ I display some text to say whether they will need to repeat the practice or not, and a code segment to break the outer loop. In this code segment, for ‘Begin experiment’, for both Python and JS code I have: ‘PLoopTally = 0’

Then in the ‘begin routine’ part I have:

For Python:
if PLoopTally >= 3:
Practice_rounds.finished = True
msg = “Well done!”
elif PCorrs >= 6:
Practice_rounds.finished = True
msg = “Well done!”
PLoopTally = PLoopTally + 1
PCorrs = 0
msg = “Let’s try that one more time!”

So this will quit the loop automatically if the outer loop has been repeated more than 3 times, or it will quit the loop immediately if at least 6 correct answers were given during the practice trials. the text after ‘msg’ is just what I display with this screen for 2 seconds.

For JS I have:

if (PLoopTally >= 3) {
msg = “Well done!”;
trials.finished = true;
} else if (PCorrs >= 6) {
msg = “Well done!”;
trials.finished = true;
} else {
trials.finished = false;
PLoopTally = PLoopTally + 1;
PCorrs = 0;
msg = “Let’s try that one more time!”;

Hi Claire,

Thank you for your detailed response. I followed the same steps as you have described, but unfortunately, I still can’t initialize the experiment online.

Could you please check what is going wrong here?

Please set your experiment to public if you’d like someone to look at it.


I am sorry for forgetting to do that. Made it public now.
Here is the link to the gitlab project:

Hi Surabhi,

I am not sure what’s going wrong with your experiment. I feel if JS doesn’t initialise it usually means there is some kind of error in your code (could be as silly as missing a bracket somewhere).

I would suggest you:

  1. change your excel list for trials (e.g.‘LL_main.xlsx’) to a csv file. Then check if there are any quotation marks in there at all. I also see you’re using image files on some of the trials, but left this blank on others. I would replace any trial where you’re not using images with a transparent image, e.g. named ‘blank.png’, which you could just make in a free software like GIMP. I would also maybe recommend using either .jpg files or .png files, but not sure if that would cause any issues. It could be that the program doesn’t know what to do for your other trials where you just left the image blank,e.g. after line 26. You can also quickly trial if this is the problem by changing that excel/csv file to just use the first 5 trials. If it initialises then, you know that was causing the problem.

What might also be causing the problem is if you’re trying to change the location where a stimulus is shown dynamically online. I am not sure what the letters refer to in your excel file, but if you’re trying to use that as a location variable that might not work.

  1. if above doesn’t change anything, start an entirely new experiment with just these practice loops in the most basic form (e.g. only show some text as a trial, very simple excel file or .csv file as trial list (no images)). Then try to recreate the practice loop you want step by step and trial it online.

Generally I would just recommend you try to get your task online step-by-step. Offline Psychopy tasks and functions don’t translate directly to online tasks. So don’t expect to be able to program your whole task offline and then for it to run perfectly online. It’s better to build and add to your task iteratively, e.g. set something up offline, then trial if that part works online. That will also allow you to figure out when and what exactly is going wrong. That’s the best advice I can give you. Good luck!

Is it currently set to piloting? If not, please could you do so, so I can try running it from

One thing could be the duration in the sound component. Try deleting that.

Hi Claire,

So, the mistake was with the quatation marks displayed in the ‘msg’ variable. The text had both a sinle as well as a double quotation mark in a sngle statement, which created a problem in JS code.
It has been resolved now. Thank you so much for the feedback :slight_smile:

1 Like


I have debugged the experiment. Do you still want to look into it?

Hi Surabhi,

Great that you solved it and your task is working now! :slight_smile: Happy to help