Conditional Branching not working for RAT experiment

**Psychopy 3.0.7
**Standard Standalone

I am in need of advice.

I am currently conducting an online experiment of the Remote Association Test. This is where participants are shown three words and their task is to type a root word that linked all three words. An example is ‘cottage / swiss / cake’ is presented on the screen. Then they will type the correct word linked, which is the word ‘cheese’. l have already built this is Psychopy on builder view with an xlxs file.

However, I wanted to know how to build the experiment so that it presents to different questions depending on if the participant got the answer right or wrong.

If the participants answered the correctly, they will be presented with a question of ‘how did they come up with the answer’.

If they answered incorrectly, they would be presented with a question saying ‘did you understand the problem’.

1)Does anyone know how to program the experiment where it recognises the recorded answer is either correct and incorrect?

  1. Also if the experiment can lead into two different paths. Question1 is presented after a correct answer and Question2 is presented for incorrect answer.

Please let me know you thoughts. Anything changes on the xlxs file, adding different loops or routines?

Thanks for your help in advance!!!

So you presumably have the correct answer in your .xlsx file, let’s say in a column labelled correct_answer, and presumably you are also capturing the typed response, let’s say in a variable called typed_answer.

So in your routine where you will present the follow-up question for an incorrect answer, insert a code component and put something like this in its “begin routine” tab:

# only run this routine if the answer was wrong:
if typed_answer == correct_answer:
    continue_routine = False

and similarly in the routine for the correct answer:

if typed_answer != correct_answer:
    continue_routine = False

Hey Micheal,

Thanks for your help. I have copied your advice. However, I am still having issues running the experiment. For some reason, when I run the experiment, Psychopy still shows both message responses, regardless if I put the correct or incorrect answer.

I have created 4 main routines which is circled by one loop. Experiment Trial 1 (where participants are shown the three associated words and input their text answer), Correct Answer message, Incorrect Answer Message and Pause block

In the Correct answer Message routine, I have added the code component. In the Begin Experiment tab it says (inputText = ") In the Begin Routine tab it says…

# only run this routine if the answer was correct:_
if inputText != corrAns:
continue_routine = True

In the Incorrect answer Message routine, I have added the code component. In the Begin Experiment tab it says (inputText = ") In the Begin Routine tab it says…

# only run this routine if the answer was wrong:
if inputText == corrAns:
continue_routine = False

I also have a textInputTest.xlsx file which has two columns named Word ( for the question) and corrAns (for the correct solution) which is attached to the loop around all routines.

Could you let me know what I may be missing, which is not allowing me run the experiment properly?


I missed that you are wanting to run the experiment online. In that case, you need to provide Javascript code that does the same thing as the Python code (in the code components, select “Both” rather than just “Py” so that you have Python code for running locally, and JavaScript for running online). The best thing to do is get the Python code running properly locally, and then at that stage, translate it into JavaScript for online execution.

1 Like

Hey Micheal,

You were right the first time. I have downloaded the Psychopy application. I have Psychopy version 3.0.0.b11 on a Windows 10. The code component is still not working. I am currently using this.

Correct Routine Code Component in Begin Routine tab:

#only run this routine if the answer was correct:
 if Typed_Answer1.keys != corrAns:  
  continue_routine = True

Incorrect Routine Code Component in Begin Routine tab:

#only run this routine if the answer was wrong:
if Typed_Answer1.keys == corrAns:
    continue_routine = False

Any advice on how to get it running? Participants are shown either the Incorrect Message routine when they type the incorrect answer. Or their are shown the Correct Message routine when they type the correct answer. The experiment still shows both routines at the moment


We really ned to see code like this, as whitespace is important in Python:

Also, can you update PsychoPy to the latest version? You are still using a beta release, and PsychoPy 3 has been undergoing continued rapid development since then to fine-tune improvements in the new features.

Hey Micheal. I have updated my previous post and downloaded the updated version. Any insight on how to get the experiment running?

My previous post has the component codes that I am currently using, which are still not working.


As above, I suggested this as the variable to test against the variable from your conditions file:

but above you are instead comparing the correct answer to Typed_Answer1.keys, which will presumably be a list of 0 or more keys collected from the keyboard component, which almost certainly won’t match the stored answer.

To understand what you are comparing, insert (temporary) debugging code like this:



Both the content and the type of the variables you are comparing have to match if they are to be found equal.

But I can’t suggest what variables you actually should be comparing, as you haven’t shown the code you are using to collect the typed response. So my suggestion of typed_answer is just a placeholder that you should replace with your actual relevant variable name.

Hey Micheal,

I have tried to use the temporary debugging code in the begin experiment tab. But it comes back with a message “Name error: Name ‘Typed_Answer’ is not defined”



Yes you are absolutely right. The typed response, needs to match the correct answer in order for the code to work properly. Not just each key that is typed in. I have defined the correct answer as corrAns that is attached xlxs in the loop. However, I do not know how to define typed answer properly in the Correct Answer/Incorrect Answer code component routines

if (???) != corrAns:  
  continue_routine = False

I have attached images of the code component in the Experiment Trial 1 routine. This is where the participants are shown the problem and type their response.

In this code component, the end routine tab states…

# let's store the final text string into the results finle...
thisExp.addData('inputText', inputText)

This records the participants answers into the data file. In the data file, the (corrAns) column needs to equal the (textInput)column. I have attached also a image of a test I have run myself showing my responses.

I have already tried to use the variable (inputText) instead, but this does not work either. Any suggestion. Really do appreciate your help.


That isn’t going to work, because nothing has been typed at the beginning of the experiment. You should try to conceptualise what the names of the tabs mean in a code component. They indicate when in the experiment that the code runs. i.e. We can’t refer to variables names until they have actually been created. So the place for that debugging code is in exactly the same code component and tab where you are already successfully referring to those variables (I think the “begin routine” tab of a code component in your correct and incorrect response routines.

The variable called inputText is definitely what you are after (i.e. the one that contains the word typed by your subject). You can see it is being stored in your datafile and so it seems to be being correctly created.

So we need to know exactly what you mean by “I have already tried to use the variable (inputText) instead, but this does not work either.”