psychopy.org | Reference | Downloads | Github

How to call a variable in a sentence?

Hi all, in a text component, I want to say ‘Congratulations, you’ve got $score points!!’. score is a variable I defined in a code component. Does anyone know how to achieve that? thank you so much in advance. /so frustrating that my psychopy book was left in the office :cry:

'Congratulations, you’ve got '+ $score +' points!!'

1 Like

Thank you so much @wakecarter!. But I got this error 'TypeError: must be str, not int '. This is must due to score. Here’s the code

if keycolor.corr:
    score += point

Do you know how to fix this error? Thank you.

$‘Congratulations, you’ve got ‘+ str(score) +’ points!!’

(the position of the $ doesn’t really matter)

1 Like

Thank you!! :blossom:

If you’re using the Python 3 version of PsychoPy (which you should be), things are even easier with Python’s f strings:

$f'Congratulations, you’ve got {score} points!!'
1 Like

It should be noted, however, that the Python 3 method doesn’t work online.

1 Like