psychopy.org | Reference | Downloads | Github

How to put condition in Text component?

Hello, I average SSDs and I show it to the subject. It works as you can see in the image below :

But I actually need to put a condition that if average is greater than a number show the subject a text beside the average number. How can I do that?
For example :

if np.average(trialsArray_1) > 100 :
    # You reaction time is GOOD because it is 105.05.
else :
    # You reaction time is BAD because it is 95.05.

The expression in the code component can only really be one line long. So it is probably easiest here to insert a code component above the text component, and in the “each routine” tab, insert multiple lines of code, based on the example you give above.

i.e. in the code, you create a variable to hold the message you want to display:

average = round(np.average(trialsArray_1))

if average > 100 :
    feedback = f'Your reaction time is GOOD because it is {average}.'
else:
    feedback = f'Your reaction time is BAD because it is {average}.'

Then in the text component itself, just put:

$feedback

in the text field, set to update on very routine.

f-strings are a feature of Python 3, so you need the Python 3 version of PsychoPy for this code to work. Otherwise, use some of the older Python 2-compatible ways of formatting strings with variables.

Use the round() function as needed to get consistent formatting of your average.

1 Like

As always great help. Thank you @Michael . :blush:

1 Like