Trying to end loop after one iteration


I am trying to get PsychoPy to play a video along with one randomly selected song from my conditions file, I want the loop to end after one iteration because otherwise it will repeat the video with each of the songs from my conditions file.

I have added:
‘myCount = 0’ to before experiment and then:
‘myCount = myCount + 1
if myCount > 1:
trials.finished = true’ to Begin Routine

However I am still getting the error message ‘UnboundLocalError: local variable ‘myCount’ referenced before assignment’.

Any ideas what I am doing wrong?

Thanks in advance for any help.

Your Before Experiment code should be in Begin Experiment.

However, you could probably just put trials.finished = True in End Routine.

Your current code will show 2 iterations.

Thank you very much, trials.finished = True gave me exactly what I wanted.


Sorry I’ve just tested with more trials and have realised that it only works for the first trial, I have put the code into the following trials but after the first trial they all repeat the video with each song in my conditions file until each one has been played.

Do you know why the code isn’t working following the first trial?
Thanks again.

This only works if your loop is called trials. For example, you might want trials_2.finished = True

However, it’s good practice to reuse routines whenever possible. If the only difference between A, B & L is the spreadsheet then you should use A for all of them, in which case you would need currentLoop.finished = True

Working perfectly now, thanks so much for the help, greatly appreciated!

1 Like