psychopy.org | Reference | Downloads | Github

Setting contingency based on keyboard input

I’m trying to make a task where an image and some text is displayed to the user, and the user uses keyboard inputs to respond accordingly. Depending on what the keyboard input is, the following screen will display different text options. Conditional branching, in other words.

I have tried to address this problem by inserting the following code block:

if response.keys == CorrAns:
    feedback_1 = LeftAns_A
    feedback_2 = RightAns_A
    feedback_3 = ItemPrice_A
else:
    feedback_1 = LeftAns_B
    feedback_2 = RightAns_B
    feedback_3 = ItemPrice_B

In this code block, response.key (I believe) is the keyboard input. CorrAns is a variable that contains what the correct answer is. Therefore, if the keyboard input is the correct answer, feedback_1, 2, and 3 should be set to equal previously defined variables that are the text I want displayed if the correct answer is given. Conversely, if the keyboard input does not equal the correct answer, feedback_1, 2, and 3 will be set to equal variables that contain the text I want displayed if the wrong answer is given.

The issue is that, since the code block is set to run at the beginning of the routine, ‘response’ is not defined, and the program crashes. I have tried just inserting a previous code block to initialize ‘response,’ but that doesn’t work since whatever I initialize ‘response’ to doesn’t have the attribute .keys. I have also tried having a keyboard input be set in a previous routine. That does not change anything.

My question, therefore, is how to have response.keys equal the previous keyboard input, and only be accessed once a keyboard input is given (which doesn’t happen until midway through a loop). Alternatively, if there is another way to achieve this conditional branching that doesn’t use this method, I would be interested.

I think you should have this code in End Routine and then have the feedback in the next routine.

Best wishes

Wakefield

1 Like