Loop components not repeating

OS: macOS Sonoma 14.6.1
PsychoPy version: 2024.2.1
Standard Standalone: yes

What are you trying to achieve?: I have two routines which I want to repeat until a condition is met. The first routine is Practice_event and second one is Practice_feedback_event. Practice_event has 4 components:

  1. practice_movie_key_resp_event: keyboard component to record keypresses while movie watching.
  2. isi_practice_event: text component to display “+” for 1 second before movie component.
  3. practice_movie_event: plays a movie clip of 2 minutes, during which I want to record participant’s keypresses.
  4. code_event: this custom code records the number of keypresses, and checks if its below or above a range, then it updates a feedback_message_event accordingly to display in the next routine (Practice_feedback_event). If the count of keypresses is not in the permissible range, then I want the routine to repeat itself. So, I included a loop around these two routines. Code:

#begin routine:
key_press_count_event = 0
feedback_message_event = “No feedback available”
upper_limit_event = 5
lower_limit_event = 2

repeat_practice_event = False

#end routine:

Count total keypresses recorded during the routine

if practice_movie_key_resp_event.keys: # Check if any keys were pressed
key_press_count_event = len(practice_movie_key_resp_event.keys)
else:
key_press_count_event = 0

Set the feedback message based on the count

if key_press_count_event > upper_limit_event:
feedback_message_event = “Too many keypresses! Follow the instructions more carefully.”
repeat_practice_event = True
elif key_press_count_event < lower_limit_event:
feedback_message_event = “Too few keypresses! Be more engaged.”
repeat_practice_event = True
else:
feedback_message_event = “Good job! Your data has been recorded.”
repeat_practice_event = False

Debug: Print the value of repeat_practice to check if it’s True or False

print(f"Repeat Practice: {repeat_practice_event}")

Save keypress data for logging

thisExp.addData(‘key_press_count_event’, key_press_count_event)
thisExp.addData(‘practice_movie_key_resp_event.keys’, practice_movie_key_resp_event.keys)

Now in the second routine Practice_feedback_event after this one there are two components:

  1. feedback_event: text component which displays $feedback_message_event from previous routine.
  2. code_feedback_event: custom code component to evaluate the repeat_practice_event flag from previous routine based on number of keypresses, if the number of keypresses was not in the specified range then the flag was set to True so that the practice_loop_event would repeat, and if not then the practice_loop_event.finished = true to end the loop. Code:

end routine:

What did you try to make it work?:
I added print statements to debug the loop. I printed the flags and count of keypresses, and they were updated well, but the movie component and the whole Practice_event routine was not reporting in the loop. Instead the number of reps I set in the loop, those many times my debug statements in custom code code_event in Practice_event routine was printing but movie was not displayed.

What specifically went wrong when you tried that?: Practice_event routine was not repeating in the loop, specially the movie component.

What is the start condition of the movie component?

The movie starts when the isi_practice_event ends,
condition: isi_practice_event.status==FINISHED. I tried putting the code components in both routines at the top, but still didn’t work. However, the feedback message in “Practice_feedback_event” routine, seems to be displayed for the time proportional to number of reps I put in the “practice_loop_event” loop. I don’t know why instead of ending the message and repeating the “Practice_event” it just keeps displaying the message.

What is the duration of isi_practice_event?

Can you put this as the start time for the movie instead of using a condition?

EDIT: After a couple of re-runs it stopped working. Previously number of reps in the loop was 9999, when I changed it to 2, it didn’t work and reverted to the original problem described above. Then I changed the reps back to 9999 and it still didn’t work. I did not change anything else in the experiment except the text contents.

Thank you so much!
Removing the condition and simply specifying the absolute start times and durations worked. However, I don’t understand why that was hindering the loop. To answer your question, the duration of isi_practice_event was 1 second. I have now replaced the conditional start for the movie with a fixed start time of 1 second.

Try upgrading to 2024.2.4 or setting the version to 2024.1.4.

I removed the isi_practice_event, and it worked. Then, I upgraded to PsychoPy 2024.2.4 and reintroduced the isi_practice_event to test if it works with this version. However, the same issues occurred as before. So, I removed the isi_practice_event again to ensure that the loop and video display function correctly.