| Reference | Downloads | Github

Conditional Branching & Key Response


We are trying to design an experiment using a two box choice paradigm. In our experiment, the participant is asked if they want to open a box or not. If they select yes, they are shown an open box. If they select no, they are shown a closed box. So essentially, a left key is used for not opening the box and right key to open the second box. We are having problems with the code, our key responses don’t seem to be linked to our loops. We are using an if statement for conditional branching, but regardless of the key we press the experiment skips to the closed box and will not show the open box.

Thanks in advance.

We can’t help without seeing what you are actually doing: please provide sufficient detail.

When writing a post in a forum like this, it is a good idea to ask yourself “If I was a person who knew nothing about this experiment and the problems with it, could I give useful suggestions, knowing nothing other than what is typed in this message?


You are right sorry about that. So the keys ‘1’ and ‘2’ do not correspond with their respective images. Key 1 is supposed to lead to routine openbox_2, and key 2 is supposed to lead to closebox. We suspect that response.keys is a list, however we don’t know how we could access the last element of the list, which would be the participant’s response. response.keys[-1] and response.keys[0] both generate a list index out of range error. Thank you.

When trying to debug issues like this, it is useful to see what you are actually dealing with. e.g. put these (temporary) debugging lines before your code:


I can’t see the settings for your response keyboard component, but maybe it is possible that the routine can end with no response being given, and hence no valid list being available?

Lastly, you seem to be using an old-fashioned approach to selecting routines (by surrounding them with a loop and setting the nReps value to 0 or 1). That was always just an ugly work-around and we can more directly control this now by inserting a code component on the relevant routine and putting something like this in the Begin routine tab:

# only run the routine if the response was a 1:
if response.keys[0] != '1':
    continueRoutine = False # don't even start this routine

This solution worked form me. Is a conditional branching, but in a source memory paradigm.