| Reference | Downloads | Github

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

URL of experiment:

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:


for x in range(12):

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


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.


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.



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.