psychopy.org | Reference | Downloads | Github

"# is read-only" error when changing array value

URL of experiment: https://pavlovia.org/valsecchimat/captureprob

Description of the problem:

Dear all,

I am building an experiment where properties of stimuli in an array are stored as vectors. My python code (which works both in python and online) is as follows:

THECOLS=[]
THESHAPS=[]
THESHAPC=[]
THETAROR=[]

for x in range(12):
THECOLS+=[iscolred]
THESHAPS+=[isshapsq]
THESHAPC+=[-isshapsq+1]
THETAROR+=[Math.floor(Math.random()*2)*90]

However, if I try to modify just one element of the arrays:

THESHAPS[tarpos]=-isshapsq+1
THESHAPC[tarpos]=isshapsq

This works in psychopy, but online I get errors like TypeError: 11 is read-only.
The number (in this instance 11) seems to correspond to the value of tarpos. I get the same error if I try to put say directly a value in the array, es.

THESHAPC[tarpos]=1

I really don´t understand what´s going on because the js code:

THESHAPS[tarpos] = ((- isshapsq) + 1);
THESHAPC[tarpos] = isshapsq;

seems to be correct as far as I can tell.

Does anybody have an idea what the problem might be?

Thank you in advance,

The issue is that you can’t use THESHAPS+=[isshapsq] to append to an array. You need to use .append. Please check my crib sheet in the pinned post for how to globally set append=push to allow the code to work in both Python and JS.

Thanks! indeed the problem was the += syntax.

Psychopy wouldn´t translate automatically the .append command to .push in JS though, so I set it by hand, but that solved the problem.

Cheers,

Matteo

As I implied in my post, you don’t have to manually change append to push every time if you follow the advice for code_JS in my crib sheet.