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:
- practice_movie_key_resp_event: keyboard component to record keypresses while movie watching.
- isi_practice_event: text component to display “+” for 1 second before movie component.
- practice_movie_event: plays a movie clip of 2 minutes, during which I want to record participant’s keypresses.
- 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:
- feedback_event: text component which displays $feedback_message_event from previous routine.
- 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.