Using a variable in the key response correct answer field


I am creating an experiment where my participants see a statement followed by an image and they have to respond with either ‘h’ or ‘b’ to say whether the statement is true or false.

I need to counterbalance the responses so for half, they press ‘h’ for true and ‘b’ for false and vice versa. Originally I had two excel files and a piece of code that worked out if the participant number was odd or even and selected the corresponding file, but I’ve been told to just use one.

I tried adding two correct answer field in my excel sheet (CorAnsEven and CorAnsOdd) and using the code from before to set the CorAns variable be equal to the relevant column (so if participant number is odd, CorAns = (“CorAnsOdd”) and then using $CorAns in my keyboard correct answer field. I tried changing the text to have $ in front (e.g., (“$CorAnsOdd”)) but both ways when I try and run my experiment all my responses are marked incorrect.

My psychopy version is 2023.1.2 and OS is Windows 10.

I appreciate any advice anyone can give and can provide further detail if you need them

Thank you.

Screenshot 2023-11-21 163157

Hi @ATS,

I think your CorAns might be saving the strings "CorAnsEven" and "CorAnsOdd", instead of the values “h” and “b”. You might want to try CorAns = CorAnsEven without the quote marks or parentheses.

If you save your CorAns variable to your data, you can check what your CorAns values are. You can use exp.addData() for this.

## After your if-else statement
exp.addData('CorAns_from_code', CorAns)

Hope that helps,

1 Like

The code will need to be in Begin Routine not Begin Experiment.

I use thisExp.addData not exp.addData

1 Like

Hi Shabkr,

Thank you that worked :slight_smile:

1 Like

Hi Wakecarter

Thank you