psychopy.org | Reference | Downloads | Github

Repeat a loop under a certain condition

If you’d have a solution for this in the Builder or the Coder, any solution is welcome.

I’ve done a code with one loop. In that loop, the subject needs to hit one correct button for the presented stimulus, but several are accepted. I want that the loop is repeated if either too many errors have been made (too many times the wrong key was hit) or if the subject took too much time to answer (judged to be longer than 800 ms after stimulus onset in my study). Check the attached code and run it once, and u’ll see two messages popping up in function of if and when you provide your answer, then you’ll know my evaluation criteria. Speaking in terms of the experiment, once the entire block is finished (ran through all stimuli once), one of two messages should show up, according to response evaluations made :

a) You hit the wrong button in more than 50% of the trials, which is why this loop will be repeated (and the loop will be relaunched from the beginning).

b) You hit the wrong button in less tan 50% of the trials, so you successfully completed this block (pass on to next block).

Same for response latency > 800 ms evaluation:

a) To more than 50% of the stimuli, you answered with a too slow reaction time, which is why this loop will be repeated (and the loop will be relaunched from the beginning).

b) You answered fast enough over all trials, so you successfully completed this block (pass on to next block).

Instant_Errormessage.py (9.8 KB)

Any help is heavily appreciated, thanks!

Hey There,
I don’t know if it’s still relevant, but here’s my attempt at an answer.
You will most likely need to put each block of stimulus presentations in a loop that you have to break out of if the conditions are met to carry on to the next block. This can either be an infinite ‘while True:’ loop, or ‘for _ in range(3):’ if the participant has a maximum of three attempts at each block.
You then need to save wheter the criteria have been met for every trial and check them at the end of the block.
Here’s a mockup example code:

rt = [] #Reaction times
correct = [] # Wheter the trial was answered correctly

while True:
    for this_stim in my_stimuli:
        # I just assume you get the RT and wheter the answer was correct somehow:
        this_rt, this_correct = this_stim.present_stimulus()
        rt.append(this_rt < 800)  # This will be either True or False
        correct.append(this_correct) # Also either True or False
    # All done with presenting, now let's check
    # Python will interpret Trues as 1 and False as 0 here:
    if sum(rt)/len(rt) < .5 or sum(correct)/len(correct) < .5:
        print('Try again!')
    else:
        break  # This is what gets us out of the "infinite loop" and to the next block

Depending on how you store your stimuli you can wrap all that into another loop where you load each block of stimuli, but that’s up to you.
I hope this helps. I’m sure you could also do that somehow in the builder with code blocks, but it might be easier to do using just code. I also didn’t really have time to dig into your code, so I hope I understood everything correctly.

Cheers