Show stimulus until participant is correct

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Mac (14.1.1)
PsychoPy version (e.g. 1.84.x): 2023.2.3
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?:
Continue showing the stimulus until the participant gives the correct key_resp, while only recording the
first response in the results file.

What did you try to make it work?:
I am able to continue looping through the stimulus until a participant gets it correct. However, when the
experiment is finished, the results file shows that the participant was correct on all trials.

What specifically went wrong when you tried that?:
Include pasted full error message if possible. “That didn’t work” is not enough information.
There is no error message. I just want to only save the first response that a participant makes for each stimulus in the results .xlsx. And, not all of the response that are made while the participant is trying to get the trial correct.

Above is a screenshot of my experiment. I have tried adding an inner loop around “Stimuli” and “Feedback” and continue looping until participant is correct. Then, manually exiting using:
if key_resp.corr:
your_inner_loop_name.finished = True

Any help on only saving the first response for each trial, while still repeating until participant is correct?

Hi @scrambleCoder,

Have you tried setting your key_resp component so that it doesn’t end the routine? Then setting the drop down in the data tab to “first key”, whilst using your code to end the inner loop when the response is correct?

If these don’t work would you mind sharing your .psyexp file so I can take a closer look?



Hi Kim,

When I do that, the stimulus never changes, even when I make a response, because I have the stimulus remain for an unlimited amount of time (which is what I want). I do have “first key” selected for key_resp… Here is my .psyexp file.

Thank you.
Go_Switch_Task.psyexp (146.2 KB)

I would use a keyboard component recording all keys and then check the accuracy of the first answer and end routine in a code component.

Each Frame

if key_resp.keys:
     if key_resp[-1].corr:
          contineRoutine = False

End Routine

if key_resp[0].corr = 1:


I appreciate the help! Here is my new .psyexp file. When I make a response, it says that Keyboard component is not subscriptable, even if I turn on “record all keys”. Do I need a list that manually keeps track of the keyboard responses?

Go_Switch_Task.psyexp (148.0 KB)

Sorry – I’ve made this mistake before. Put the [-1] and [0] after .keys and .rt instead of before it.

When I make that change, I get the error:
if key_resp.keys[-1]:
IndexError: list index out of range

right now, I am putting in a new code component into my “Stimuli” route and adding the code suggested above. Is this not the right way to go about it?

My code didn’t refer to.keys[-1] Where does that appear in your code?

Here are screenshots of the new code component that I added, called “testing”. I am leaving out the reaction time data for now.

if key_resp.keys:
     if key_resp.corr[-1]:
          contineRoutine = False
if key_resp.corr[0] = 1:

When I do that, I get the error " if key_resp.corr[-1]: TypeError: ‘int’ object is not subscriptable"
Would I need a list that keeps track of the key_resp.corr for each key response, check whether list[-1] is correct?

Have you set it to save all keys? If so, I think this may be a quirk of slice. Try:

if key_resp.keys:
     if len(key_resp.keys) > 1:
          if key_resp.corr[-1]:
               contineRoutine = False
     elif key_resp.corr:
          contineRoutine = False
if key_resp.corr[0] = 1:

I get the same error: “if key_resp.corr[-1]: TypeError: ‘int’ object is not subscriptable”
I made sure that it is set to all keys.

Have you set the correct answer in the component?


I’ve done some investigation.

key_resp.corr is based on the first answer and doesn’t create a list. Therefore the best was to do this is to use the following code in Each Frame

if key_resp.keys:
    if key_resp.keys[-1] == corrAns:
        continueRoutine = False

where corrAns is the variable with the correct answer you have put in the keyboard component.

Now, the routine will continue until you make the correct response. But, the excel file that contains the results will show all incorrect.

Based on my investigations it should show correct if your first answer is correct.

It seems to be an issue with “all keys” versus “first key.” When I choose all keys, everything is considered incorrect in the results file. And vice versa, when first key is on, everything is correct

In that case, you could add the following in End Routine

if key_resp.keys[0] == corrAns: