Reference error: sum isn't defined

when I use the pavlovia online experiment, the experiment always stucks in this line : reference error : sum isn’t defined. what I wonder is sum is a built in function, so why do I need to define it ? And how can I define it ? Pls help me ,thank you .

Hi there @banana,

Could you please confirm:

  1. Which version of PsychoPy you’re running
  2. The line/block of code where you’re using sum(), please copy and paste in a code block here if you can
  3. Are you using the Auto>JS code type or typing directly into JS yourself?

Thanks,

Kim

thank you for your reply
1.version:pyschopy 2020.1.3
2.if key_resp_practice.corr == 1:
total = total + 1
feedback = ‘correct’
else:
total = total
feedback = ‘false’
a.append(key_resp_practice.corr)
b = sum(a[0:])/total
if total >= 6 and b > 0.9:
trials.finished = True
feedback = ‘Practice is over,\nNow the experiment is starting.\nPlease pay attention.’
3.Referred to someone’s code, I want to calculate the accuracy rate so did some changes.

Hi @banana,

Thanks so much for this! So, firstly I would recommend updating to the latest version of PsychoPy here. Next, if you set your code type to Auto>JS (in the drop down at the top of the code component), sum should auto transpile to util.sum on the right hand side of the code component (see screenshot).

This should stop the error.

Thanks,

Kim

Hi, Kimberley,
thank you so much for this answer!
I found the latest version of psychopy didn’t support the online experiment, so I chose the available 2020version.And these codes with changes can be run in local but not on the online platforms (pavolvia, naodao).

Hi @banana,

No problem at all. The reason for these not running online is actually the same issue as you faced previously. If you now change the code type to ‘Both’ so that you can edit the right hand JS side, you can change:

a.append to a.push

and then

b = (sum... to b = (util.sum...

then it should work. Unless you’re now getting a new error online?

Thanks,

Kim

Hi Kimberlry,
thank you so much, this really help me a lot.
Online experiment shows problems and I did some detour to avoid using sum, I really appreciate your help and patience.