psychopy.org | Reference | Downloads | Github

Using a variable from conditions file in code

I am interested in how I can pull a value from my conditions file, and use it in my code to determine what kind of feedback to give.

I am running multiple trials (my loop is called ‘trials’), each trial corresponds to a row in my ‘Conditions’ file. In my conditions file, I have a column called, “Feedback”. Values in this column can be either ‘correct’ or ‘incorrect’.

If I am in a trial where the feedback type is ‘correct’ I want one type of message presented, if it is ‘incorrect’ I want another. This is an example of the code I have tried in my code builder:

if trials["Feedback"] == 'Correct':
    msg = "You got some behaviors correct" 
elif trials["Feedback"] == "Incorrect":
    msg = "You got some behaviors incorrect" 
else:
     msg = "you made a mistake in coding"

The error message I get is: TypeError: ‘TrialHandler’ object is not subscriptable

What am I doing wrong? How can I use the text in my conditions file to determine what message to display? Thank you so much for any help or guidance!

1 Like

You can use this code at the begin Routine tab:

if Feedback == 'Correct':
    msg = "You got some behaviors correct"
elif Feedback == 'Incorrect':
    msg = "You got some behaviors incorrect"
else:
    msg = "you made a mistake in coding"

However, you have decided to give participants feedback based on your condition file, not based on their actual response. Is that what you want? If not, you can simply find several example of giving feedback in the previous posts.

Thank you for your response! I simplified here and realized I neglected an important aspect. You are right, I want to give feedback based on their responses. So the message actually looks like this:
“You got %i correct.”
This means that I cannot set the response until after they have responded. Where can I put this code that is after the responding, but will still recognize the variable? I previously had it in the End Routine portion, but that is where I got the “TrialHandler” error.

That error is not due to where the code is placed, but because you are treating the TrialHandler (i.e. the loop) as if it was a dictionary, and trying to access values from it like this: trials["Feedback"]. That won’t work. It would work for an individual trial, which is stored as a dictionary, but not for a TrialHandler, which is quite a different beast.

The code David gave above addresses your bug, by just referring to the current variable name directly, i.e. by name like Feedback.

1 Like

Last year, based on the website posts, I wrote a personal guide on how to provide feedback and I thought it might help you, too.

The following examples assume you have a routine called trials, containing a Keyboard Component called key_resp and a routine called feedback.

With these in mind, you will have the following variables after every trial :

key_resp.keys #a python list of keys which were pressed
key_resp.rt #the time that the first key press was pressed
key_resp.corr #None, 0 or 1, if you are using 'store correct'

Suppose after each trial, participants were presented with feedback on their answer and response time. All you need to do is to create a code component in the feedback trial like this:

#Begin experiment tab
msg='doh!'#if this comes up we forgot to update the msg!

#Begin routine
if resp.corr:#stored on last run routine
    msg="Correct!RT=%.3f" %(resp.rt)
    txtcolor = "green" 
else:
    msg="Oops! That was wrong"
    txtcolor= "red"
1 Like