Display transformed input variable (slice error)

Hi all,

I’d like to multiply a textbox input and return the first 5 digits of the result on the dispaly, but I encounter the following errors while running it online.

in the end routine of a code component:
returnValue = 12345*int(textboxCode.text)
returnValueStr = str(returnCode[:5])
thisExp.addData(‘returnvalue’, returnValue)

1- returnValue is saved correctly in the results csv file, but it does not update during the experiment: if I try to display it, the screen shows the initial value I assign to it when I initialize the variable.
2- autotranslate returns returnValue.slice(0, 5) , which gives error when I run it in Pavlovia ( TypeError: returnValue.slice is not a function)

Thanks in advance for any help.


if you put everything in the End Routine tab, the text-component displaying the returnValue will not be updated. The routine ends. Try putting everything but thisExp.addData in the Each Frame tab.

Best wishes Jens


what is returnCode[:5]? Shouldn’t it be

returnValueStr = str(returnValue)
returnValueStr = returnValueStr[:5]

Best wishes Jens

Thanks for your reply.
I tried to put the part:
returnValue = 12345*int(textboxCode.text)
in the “each frame” tab but still it doesn’t work (variable does not update).

One solution I have found is to create a variable and then append textboxCode.text to it:
which gets translated in JS as
response = response.push(textboxCode.text);
(quick note: I have to manually write in the code part the translation because Array.prototype.append = [].push; in the begin experiment tab makes the experiment crush)

However, while I can succesfully display $response as a text in the next routine, it seems that I cannot apply any tranformation.

For example:
in the code component:
in the text field:
$response → the textboxCode.text is successfully printed
$responseTrans → nothing is displayed

Thanks again,

however it gets translated into .slice(0, 5), and when I run it in Pavlovia: TypeError: returnValueStr.slice is not a function


it would be helpful if you surrounded your code by triple `. Then it is properly formatted. I never worked with the new textbox.

Strange that Array.prototype.append = [].push; crashes. I have used it myself without problems.

Do you have a toy-example of your experiment which you could upload or give access to in your repository?

Best wishes Jens

here’s the link to the part of the experiment that I have problems with:

in this version, I print the input string and it works. I’d like to print the modified version however.

Hello Matteo,

I can’t “speak” JS so I need access to the PsychoPy-script which is not included in your repository. I usually “program” everything in PsychoPy, let it autotranslate and edit the few (hopefully) places which do not work in PsychoJS.

Best wishes Jens

Dear Jens, I’m sorry. I uploaded the .py file which has been compiled by the builder.
It should be uploaded now.

Hello Matteo,

this version runs locally. I hope it runs in the way you intended it to do. However, the program does not run on-line. The error that PsychoJS throws is state.toLowerCase is not a function. You find some information concerning the error in the post just mentioned. I have been using PsychoPy 2020.2.10. I suggest opening a new thread for this.

dummyCodeTrans.psyexp (21.5 KB)

Best wishes Jens

Hello Jens,
Thank you very much for your time! You have been very helpful.

I found a solution by writing this line in the end routine component:

response.push((Number.parseInt(textboxCode.text)*expCode).toString().substring(0, 5));

(expCode is a number) and in the text component of the next routine $response.

This works online.

Hello Matteo,

cool that it works now.

Best wishes Jens