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