Search code examples
python-3.xpsychopy

Having a break within a loop and waiting for the person to continue


Hi i am running an experiment which has 120 trials. i want to add a break in the loop every 30 trials and wait for the participants to press a key when they are ready to continue. my code for the loop looks like this

start.draw()
win.flip()
event.waitKeys(keyList=['return'])

win.flip()

cross.draw()
win.flip()
event.waitKeys(keyList=['5'])


for stim in stroop:
    colour.text = stim[0]
    colour.color = stim[1]
    colour.draw()
    display_time = win.flip()

how can i add a break into this for loop? Thank you!!


Solution

  • You can use enumerate to keep track of the number of iterations made:

    for idx, stim in enumerate(stroop):
        # The +1 makes it so we avoid asking the user's input on first iteration.
        if (idx + 1) % 30 == 0:
            event.waitKeys(keyList=['return'])
        [...]
    

    By the way, breaks in python refers to breaking out of the loop. What you want to do is rather "wait for user input".