Using conditional key responses

Hi, I’m completely new to psychopy and coding in general and trying to create an online version of the Iowa gambling task.

I have implemented 4 images of cards successfully and also key responses ‘a’,‘b’, ‘c’ and ‘d’ corresponding to which card they wish to select. What I then want to happen is if the key a is pressed, for example, the amount of money earnt/lost is displayed. what I have done so far is insert a loop in which I have imported conditions files with 4 parameters called ‘deck_a’, ‘deck_b’, ‘deck_c’ and ‘deck_d’ and in each row are the consequences of each selection made.

so I have my trial and a loop around that. i have then inserted a ‘feedback’ routine to be included in the loop after the trial so that is shows money earnt/lost.

in the feedback routine, I have inserted a text component which only starts if resp_a (pressing the letter a) is completed and it shows the money earnt/lost. i did this by putting $deck_a in the text box. i repeated this for all response a,b,c,d but the problem is that all text shows no matter what key is pressed. in my feedback routine i also have another text which constantly shows press space to continue and key response which only allows space to force end of routine and loop back round.

this worked when i trialled it with only one image but now i am working with 4, i am struggling

i would like to know how to get only the money earnt or lost through selection of cards shown if a certain key was pressed.

i understand i need a coding component but not sure where to start any help would be appreciated :slight_smile: if you need any more information i am happy to provide

Hello Harriet,

we need more information. Please include screenshots of the relevant routines and insert the code you are using to determine the feedback (surrounded by triple `).

Best wishes Jens

Hi Jens,

thanks for your response

I’ve attached what I’ve got so far, but it isn’t a working experiment, I am just trying to figure out the coding. i am using pandas but to import the csv file but I don’t believe that it works with logical statements.

all I would like to know is how to get certain feedback when a key is pressed and other feedback if another key is pressed.

the column headers in my csv file are called deck_a and deck_b hence why I have assigned them to choice_a and choice_b respectively

fb_a and fb_b have got %choice_a and %choice_b in the text box section to represent what is going on in the code also


do your participants have to give one or two answers? I am wondering whether your really need two keyboards? The attached program gives “feedback” based on key pressed (1 or 2).

feedback3.psyexp (11.5 KB)

Do you want to run the experiment online? If yes, don’t import any libraries. They won’t work online.

Best wishes Jens

hiya jens,

thats great thank you so much, the only thing is the feedback is based on many different responses from a .csv file, which is where I’m struggling

deck_test.csv (59 Bytes)

this is the .csv file, as you can see the decks at the top (a,b,c,d) correspond to keyboard press a,b,c,d. what I would like is for the experiment to run through each trial and give the feedback based on keyboard response from the .csv file but I don’t know how to code this .csv file into the coding bit, or even if I need to. so the trial would run through each row one after another.

i have previously attached the .csv file to the loop around the trail and feedback and can use $deck_a for example in the text option to give a response but I only need the response for the keyboard press at the time (I hope that makes sense, I’m sorry!!)

the plan isn’t to run the experiment online no

thank you for this again

If you have the .csv file connected to your loop so the variables deck_a, deck_b etc are available, then all you need to do is construct the variable name you need, by concatenating the prefix deck_ with the key that was pressed, and then using the Python eval() function to evaluate that string of characters, to get the value associated with that variable on this trial (i.e. your monetary values).

So you just need a single text stimulus on your feedback routine. Put an expression like this in it:

$eval('deck_' + your_keyboard_component_name.keys)

and set that field to update on every routine.

You can put a $ or other currency sign and any other prefix in front of it like this:

$'You won $' + eval('deck_' + your_keyboard_component_name.keys)

If that causes an error about concatenating strings and numeric values, you might need to do something like this to ensure you are adding two strings to each other:

$'You won $' + str(eval('deck_' + your_keyboard_component_name.keys))

f-strings make this sort of thing even easier, but that is a lesson for another day.

hiya, thank you very much for your help, I’m really new to python and coding in general and I’m not too sure I understand

this is the current format of my feedback routine

and the choices are set up like this

was this what you meant?

however i get an error and that says
File “/Users/harrietwarden/OneDrive - University of Leicester/new experiment/”, line 283, in
choice2.setText(eval(‘deck_b’ + resp.b))
AttributeError: ‘Keyboard’ object has no attribute ‘b’

so i am struggling with what to next?
i really appreciate your help, thank you!

The error tells you what is going wrong: you are referring to a keyboard object called resp and then trying to access an attribute of it called .a. But keyboard objects don’t have an attribute with that name, so an error occurs. Instead, the key that was pressed is stored in an attribute called .keys (even though it contains just one keypress in this case). So as above, you need to use code like this:

Now I’m not sure what your_keyboard_component_name should be: in your screenshot shown above, you seem to have two keyboard components, called resp_a and resp_b. You can’t have two keyboard objects active at the same time, as they will conflict with each other (unless you have two separate physical keyboards and want to collect input from them independently (and even that works only on Macs and Linux)). So delete one of the keyboard objects, and allow just the remaining one to handle your input. Use the name of that keyboard object to refer to in your code.

Looking though your code snippet above, there seems to be some confusion about how keyboard components work: you don’t need one for each possible key. Instead, you have one keyboard component, and allow it to accept whatever the valid input is. e.g. This could be a sequence of alternatives like:
'a', 'b'

If the components is set to “Force end of routine”, then only a single keypress will be stored (if it is one of the valid options), and it will be kept for you in the .keys attribute of the keyboard object.

Additionally, look again at the suggested code, which does not match what you typed:

$eval('deck_' + your_keyboard_component_name.keys)

Note that, unlike in the code in your screenshots, we don’t explicitly refer to either a or b in that code. The whole point is that we get the letter that was typed by retrieving it from the .keys attribute of the keyboard. Computers are very literal: you need to type the code exactly as suggested (in this case, the only thing you should change is to provide the correct name for your keyboard component).

You also missed this:

Again, the point here is that we are using code to decide what feedback to provide. Currently you are trying to somehow provide both alternatives at once, via separate text components, which again means the code is fighting against itself. Just provide a single text component, which figures out itself what it needs to display via that single snippet of code.