Looping: start over once incorrect

Hello! My goal is to test subjects on 18 words they’ve just learned. The words will be displayed in the middle of the screen, and subjects have to categorize them. These words either belong to category A (respond by “A”) or B (respond by “L”).

I want the order of these 18 words to be randomized in this test. Every time the subject gets one item wrong, they have to start over immediately (instead of going through the remaining items before starting over). The loop only ends once they have gotten all 18 items correct.

I have seen similar topics on here, but they are more about counting correct answers and achieving a certain percentage of accuracy. Would really appreciate some help! I would like to achieve this in the builder view if possible. Thanks in advance.

Yours will indeed be very similar. You will need a nested, two-loop structure around your trial routine.

The inner loop is connected to your conditions file, and is set to random. The outer loop is not connected to a conditions file and is set to run indefinitely (just give it an nReps that is unfeasibly large, like 100).

Let’s say your trial has a keyboard component called key_response which knows from your conditions file whether the response was correct or not. In your trial routine, insert a code component and put something like this in the End routine tab so it will run at the end of each trial:

if not key_response.corr:
    # the subject was incorrect, so terminate the inner loop.
    # the outer loop will restart it from the beginning.
    your_inner_loop_name.finished = True
elif your_inner_loop_name.thisTrialN == 17:
    # we've made it to the 18th trial and were correct. So we need to 
    # end the outer loop to avoid restarting:
    your_outer_loop_name.finished = True
    # the inner loop will end automatically as we are at the last trial,
    # so the task is now complete.
    
   

Psychopy returned that !key_response.corr: is a syntax error. I guess that’s beacause negation is not instead of !. After replacing ! with not, the error no longer occured. However, I cannot seem to get the loops to break, any idea why that might be?

Sorry, sometimes I mix up my R and my Python.

Try inserting some print statements for debugging purposes:

if not key_response.corr:
    # the subject was incorrect, so terminate the inner loop.
    # the outer loop will restart it from the beginning.
    your_inner_loop_name.finished = True
    print('Incorrect')
elif your_inner_loop_name.thisTrialN == 17:
    # we've made it to the 18th trial and were correct. So we need to 
    # end the outer loop to avoid restarting:
    your_outer_loop_name.finished = True
    print('Correct and finished')
    # the inner loop will end automatically as we are at the last trial,
    # so the task is now complete.
else:
    print('Correct')

Check what result happens on each trial, given what you know should be happening. The likely source of error will be in the Keyboard component/conditions file relationship.

I am not sure what exactly went wrong with the original method. Perhaps, .corr was the problem (I’m very new to coding, so I cannot be sure). I was able to change the code and add a counter to achieve what I wanted:

For the first part (if incorrect), I changed it to if key_response.keys != corrAns instead. Also, mycounter = 0 in this case. Then break out of inner loop with your code.

For the second part (correct), mycounter+=1. If mycounter == total number I want, then break out of outter loop with your code.

Not sure if this is the best way of doing it, but it worked! So just thought I’d share. Thanks for your help, Michael!