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?

Hello,
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.

Hello,

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']
shuffle(colorList)

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)

Hello,
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’]
shuffle(colorList)

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

textLast.color = colorList[1]

Hello,

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

Hi,

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’)

Hello,

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

Hi,

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
.shift(0)
.shift();
.shift(0);
and still get the same error

Hello,

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

Hello

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