Problem selecting specific stimuli randomly in loops (PsychoPy v2022.1.2)

Hello everyone,

I am a complete beginner in PsychoPy and I would like some advice on a particular matter. In my experiment I have inserted two loops with auditory stimuli in both of them. I want to make PsychoPy choose only two stimuli randomly for the first loop out of the ten and seven stimuli randomly for the second loop out of the twenty. I have succeeded in randomizing the order, but when I run the experiment it plays the whole table and I do not know how to tell PsychoPy to choose only two and seven stimuli respectively. I have included screenshots from the experiment in order to help you see what is going on.

Thank you!





The easiest way to do this is to have a code component that breaks the loop when a certain value of .thisN is reached.

For example

if trialsBinauralBeats.thisN == 6:
     trialsBinauralBeats.finished = True

Note that PsychoPy starts counting from 0, which is why I’ve put 6 not 7.

Thank you for your response!

I am going to try this out right away to see if it works!

Tried to run the experiment and this error appeared when it reached the first loop (image 1). I have also uploaded the code screenshot in case the position is wrong inside the loop (image 2).


Try moving the code component into your main routine within that loop rather than having it in a routine by itself. Alternatively, add something else to the routine such as a fixation cross.

Added the code inside the main loop routine and also tried to run the experiment with both an image (e.g cross) and a blank text component inside that routine as well. Still getting the same atrribute error.

What are your loops called? You errors seem to contain two different names.

Yes I found the mistake. I had different names on the section “Name” of the Accoustic Beats and the line of code. When the names matched the loops ended as intended with no PsychoPy crush or errors. Thank you very much for your time and response!!! I am including screenshots of the experiment sections mentioned above so anyone facing the same problem can immidiately check if the name sections are correct after using your advice.

Take care!