Assign colour randomly to the last word in the sentence

Hi all,

I am presenting a set of sentences of varying length in a colour detection task. I am using a code component to present each word, one by one, for 150 ms. The last word should be presented in either blue, red or green colour. However, I’m not sure how to code this. That is, how to randomly assign colour to the last word only?

Seeing your routine will help a ton.
Without seeing your routine and code component, I can suggest doing something like this for the text component:

# Start Experiment
import random 
colorList = ['red', 'green', 'blue']

# Start Routine
textLast.color = random.choice(colorList)

# 'textLast' represents the last text component.


notice that the code mentioned by Chen won’t work if you want to run the experiment online. Importing Python-libraries will throw an error when running online.

colorList = ['red', 'green', 'blue']

text.last.color = colorList.pop()

could do the job. Notice that .pop() remove the element from the list. So, this code might not work for your particular needs.

Best wishes Jens

Thanks a lot!
Here is the routine:
untitled.psyexp (15.5 KB)
and excel file:
1.xlsx (10.7 KB)

Did you try @JensBoelte solution?

Yes, I use shuffle instead of importing random. This is how:

Begin experiment:

shuffle = util.shuffle

Begin routine:

colorList = [‘red’, ‘green’, ‘blue’]

Each frame (not sure about this one, but I definitely cannot use pop):

textLast.color = colorList[1]


It not clear what kind of code you are showing us.

shuffle = util.shuffle

looks like Python code, but is unnecessary. shuffle is defined in Python. Notice that lists in Python start with 0, not with 1.

Your code does not compile. For instance, textLast.color is undefined. I am not quite sure but I assume that

Best wishes Jens


I use

shuffle = util.shuffle

because if I don’t I get the following error:

  • ReferenceError: shuffle is not defined

I worked on this code and now it works perfectly offline: it presents my sentences word by word and separately presents the last word in a randomly assigned colour.
1.xlsx (11.1 KB)
untitled.psyexp (20.3 KB)

However, running it on Pavlovia gives the following error:

  • TypeError: Cannot read properties of undefined (reading ‘pop’)


which PsychoPy-Version are you using? There is, according to the crib sheet, no need to define

shuffle = util.shuffle;

in versions 2021.1.3 onwards.

BTW, notice the ; at the end of the line. This is needed in JavaScript but will throw an error in Python. I
thought that your were defining shuffle in Python because of the missing ;.

.pop() will throw an error online. You need to manually edit the JavaScript-code to .shift(). See the crib sheet.

Best wishes Jens


I switched to 2022.1.3 version.
I also changed .pop(0) to .shift() and I am constantly getting this error now:

  • TypeError: Cannot read properties of undefined (reading ‘shift’)

I tried:
.shift() = as crib sheet suggests
and still get the same error


you need to specify the array that you what to shift an element from. See here

Best wishes Jens

Thanks, I 've read about this too, but I’m not sure how I can do that.
I added this line, but it did not help:
let sentence_list = [];
Neither did this:
const sentence_list = [];

sentence is a variable which contains different sentences from my conditions file


well, looks like you are completely lost. It doesn’t make sense to try just something. Try to solve one problem after the other. Make the experiment simpler.

I don’t see how
let sentence_list = [];
would help with the .split() problem.

Best wishes Jens