ReferenceError: variable is not defined - local variable within loop

Hello, so I currently have a code snippet that looks like this inside start routine (There is also a textbox that just waits for 5 seconds):


finalNumber = 1
for i in [0,1,2,3]:
    testCase = 32
    finalNumber = finalNumber + i

This works completely fine locally, but online it throws an error. Do I have to go into the javascript side and set the scope for testCase?

Hi. This should help.

Ah I see, so I assume it is just good practice to make everything global then. Is it just a case of be careful with variable naming instead of using scopes?

Also thanks for the link, I haven’t seen that one before.

I think that the issue is that most PsychoPy variables in Python have global scope and JavaScript don’t use variable scope in the same way.

1 Like