Help with force ending a routine with keyboard component

Hello,

I am building an experiment where the participant sees 6 images and then must choose a correct image by pressing one of the keys on the keyboard (1-6).

For certain trials, there are 3 possible correct responses.

As the keyboard component only stores 1 correct answer, I have added 3 keyboard components for trials with 3 correct answers. To clarify, only 1 response is needed per routine.

The problem is, when I have all the 3 keyboard components with the “force end routine” box checkmarked, psychopy will only store the response of the first keyboard component and will immediately end the trial afterwards. This is problematic as if the participant chooses a correct answer (which is stored in keyboard component 2 or keyboard component 3) the correct answer will not be stored in the data file.

The possible answers are ‘1’,‘2’,‘3’,‘4’,‘5’,‘6’
The three correct answers are ‘4’,‘5’,‘6’

Is there a solution to this problem?

Thanks in advance

3 keyboard components :

Keyboard component 1 :


Keyboard component 2 :


Keyboard component 3 :


Hi,

as you have noticed all keyboard-component compete for the response and end the trial. You should use only one keyboard-component and determine the correctness of the answer either with code in PsychoPy or later during analysis.

if key_resp.keys == '4':
    key_resp.corr = 1 
elif key_resp.keys == '5':
    key_resp.corr = 1 
elif key_resp.keys == '6':
    key_resp.corr = 1 
else:
    key_resp.corr = 0

You might have to add

thisExp.addData("key_resp.corr", key_resp.corr)

Best wishes Jens

Hello,

Thank you very much for your reply.

I have put all 3 possibles answers into one keyboard component as you suggested.

I am unfamiliar with the coding in psychopy so I am unsure as to where I should put this code. For example in the screenshot I put the “code” module at the bottom of the routine, and I placed the python code you provided into the tab, “end routine”.

Where is the correct placement of this code?

When I am running my experiment, it crashes when I get to this routine with the code with the error : “name “key_resp” is not defined”



I read the help page regarding the code component.

Please correct me if I am wrong but, since I am coding for storing the correct response at the end of the routine, I should put the code in the tab “end routine”, and I should put the code component at the very end of my routine.

I have tried altering the positioning but I am still getting an error that key_resp is not defined.
I tried to remove the line of code - thisExp.addData… and I am getting the following error
Screen Shot 2022-12-20 at 17.23.20

Hello,

You get the error message because your keyboard-component has a different name. You need to replace key_resp.corr with the name of your keyboard-component, answt7.corr. Key_resp is the name PsychoPy suggest when insert a keyboard-component.

The placement is ok, after the response have been given in the End routine tab.

Best wishes Jens

Thank you for your continued help.

The code is running now, but for some reason I am still getting a 0 instead of a 1.

Here is the keyboard component:


And here is the code:

And here is the output in my data file:
Screen Shot 2022-12-22 at 10.37.04

Hello,

AFAIK, you need to specify the correct answer via a column in the Excel-file. Add the column-name containing the correct answer in Correct Answer of the keyboard-component. Your approach does not work. In addition, using Correct Answer allows you only to specify one correct key per trial not three, AFAIK. If there are three potentially correct keys, you the code I send you. The line

thisExp.addData("answt7.corr", answt7.corr)

has to follow the if-else construction. Currently it saves the correctness of the previous trial.

It might be better to start with a simpler example, e.g. just one correct key, and when this works, expand it to more correct keys.

Best wishes Jens

Thank you for your response. Unfortunately I didn’t use an excel file in my psychopy experiment. I understand psychopy is continuously in development, but the fact that multiple correct answers cannot be stored in a single keyboard component without using an excel file seems to me to be quite a missed opportunity.

Code components can be used to allow experiments to go beyond the standard Builder options.

You don’t have to use a spreadsheet to add a Correct Answer to a keyboard component. However, if you want more than one correct answer then you need to use a code component.

In End Routine you can have something like:

if '1' in resp.keys or '3' in resp.keys or '5' in resp.keys:
     thisExp.addData('Score',1)
else:
     thisExp.addData('Score',0)

Hello

@wakecarter is quite right. Some code helps here. My reference to a column in an Excel-file refers to the case in which there is only one response correct for a given trial, not multiple responses.

Best wishes Jens