33  Giving feedback

Sometimes you might want to give participants feedback on their performance. In particular, this might be the case while they are still practising the task, as shown in the flow of our flanker task:

The flow from the flanker task.

In this example, participants will be presented with a routine called feedback after every practice trial. The problem is that you cannot know what type of feedback (e.g., correct or incorrect) will be required in advance, because the feedback will depend on the participant’s response. Therefore, you will need to figure out what feedback to give on the fly. To achieve this, you need to add a Code component (located in Custom components) to the feedback routine:

The Code component icon.

Code components allow you to insert Python code into an experiment. You don’t need to learn how to write Python code to be able to do this, but you need to know a few Python basics. I’ve compiled these basics in Appendix B. If you have no previous experience using Python, you should have a look at this chapter before starting to work on the exercise.

33.1 Accuracy feedback

The basic idea is this:

  • You have a routine where responses can be either correct or incorrect.
    • In the Keyboard component of this routine, you tell PsychoPy to store whether or not a response was correct.
    • PsychoPy automatically codes correct responses as 1, and incorrect responses as 0.
  • In a subsequent routine, you use the information that PsychoPy has stored on the previous routine to provide the participant with feedback.
    • If the response was correct, you present positive feedback.
    • If the response was incorrect, you present negative feedback.

Let us go through how this works in our letter flanker task. Please open the flanker task from Lab 5 in PsychoPy and have a look at the relevant routines and components:

  • Routine trial
    • The Text component presents the stimulus.
    • The Keyboard component records the response.
      • If we told it to, the Keyboard component will store the accuracy of the response. → This is the information we need to give accuracy feedback!
  • Routine feedback
    • The Code component checks whether the response was correct or incorrect.
    • Depending on the accuracy, the Code component updates the message to be shown as feedback.
    • The Text component presents the feedback message.

Here is what we need to know to make this work:

  • The name of the Keyboard component that stores the accuracy of the response. In our flanker task, this is response.
  • PsychoPy stores the accuracy information in a variable called <nameOfResponseComponent>.corr
    • The text between the angle brackets is a place-holder for the actual name of the component.
    • In our example, the relevant variable would be response.corr.
  • The feedback message itself is some text that needs to change from trial to trial. Therefore, it needs to be variable. In our flanker task, the variable is called fbMsg.
  • After the Code component has figured out what the feedback message should be, we would like to present the feedback to the participant using a Text component. We thus need to add $fbMsg to the Text component presenting the feedback in the routine feedback and set it to set every repeat.

Using fbMsg in the Text property combined with 'set every repeat'.

Now, let’s look at the actual Python code. We need two pieces of code in two different places. The first piece of code is this (you can simply copy and paste the code into your experiment!):

fbMsg = "Argh! It's not working!"

This needs to be placed in the “Begin Experiment” tab of the Code component:

Python code in the Begin Experiment tab.

The aim of this is to define the variable and to assign a value to it that will help you figure out whether or not your feedback is working. You could simply define an empty string, but this will make it harder to determine what is going wrong if the feedback doesn’t work as intended:

fbMsg = ""  # possible, but not useful!

The second piece of code is this:

if response.corr == 1:    # if the response was correct
    fbMsg = "Correct!"    # this should be our FB message
else:                     # alternatively (i.e., the response was incorrect)
    fbMsg = "Incorrect!"  # this should be our FB message

This needs to be placed in the “Begin Routine” tab of the Code component:

Python code in the Begin Routine component.

Note that the order of components is important! In the routine feedback, the Code component needs to be above the Text component, otherwise the feedback will not work (see Section 34.1):

Component ordering: The Code component needs to be above Text component.

33.2 Too slow feedback

For some experiments, you might also want to give “Too slow!” feedback if a participant does not respond before a set RT deadline.1 In this case, you need to slightly modify the above approach, because PsychoPy codes both “wrong” responses and “too slow” responses as 0. Thus, we also need to check whether or not a key was pressed to decide if the feedback should be “Too slow!” or “Wrong!”. This is the code:

if response.corr == 1:  # correct response
    fbMsg = "Correct!"
elif response.corr == 0 and response.keys != None:  # incorrect response given
    fbMsg = "Wrong!"
elif response.corr == 0 and response.keys == None:  # no response given
    fbMsg = "Too slow!"

How does this work?

  • PsychoPy stores the information about pressed keys in a variable called <nameOfResponseComponent>.keys.
  • == means is equal to and != means is not equal to.
  • In Python, None (capitalisation is again important here!) means “no value”; here it means that no response keys were pressed.

You could replace the second elif statement by simply saying else:, but being more verbose sometimes makes it easier to understand what the code does.

33.3 Giving feedback quiz

If the Keyboard component is named ‘response’ and ‘Store correct’ is ticked, what is stored in the variable ‘response.corr’?

The correct answer is that response.corr stores a value of 1 for correct responses and 0 for incorrect responses. This is PsychoPy’s automatic coding system for response accuracy, which can then be used to provide appropriate feedback to participants.

In a PsychoPy feedback routine, what is the essential component ordering requirement?

The correct answer is that the Code component must be placed above the Text component. This ordering is crucial because PsychoPy executes components from top to bottom, and the Code component needs to determine the feedback message before the Text component can display it.

What are the key requirements for implementing accuracy feedback in PsychoPy? Select all that apply.

All of these elements are required for implementing proper accuracy feedback in PsychoPy. The Keyboard component records the response accuracy, the Code component processes this information to determine the appropriate feedback, the Text component displays the required feedback, and the input file tells PsychoPy what the correct answer is.

If you want to present participants with ‘Too slow’ feedback, which of the following statements are correct? Select all that apply.

Statements 1, 2 and 4 are correct. The feedback routine does not require a key press (it can simply be presented for a pre-determined length). The trial routine on the other hand does of course need some form of response.


  1. Note this requires that your trial has a defined Stop time. Otherwise it will never end and participants can not be too slow.↩︎