NameError: name not defined (even though it was defined earlier)

PsychoPy v2020.2.8, running on MacOS big sur v11.1. Pavlovia run from Google Chrome v87.0.4280.141 (x86_64)

Hello all, I have an experiment I am doing in Builder and that I am subsequently putting online. The issue I am having lies in a variable not being recognized. Before the experiment I define in a code routine in the Begin experiment tab the following:

In python:

listInput = expInfo['participant'] + expInfo['GENDER'] + '.csv'

In JS:

listInput = ((expInfo["participant"] + expInfo["GENDER"]) + ".csv");

Later, in my experimental loop, in conditions I write $listInput. This is so that based on the participant, a different .csv file will be selected. e.g., participant 1m gets 1m.csv and so forth.
This works on Builder great, ONLY when I use the Python code. When I use the JS code, the experiment runs until I get to my experimental loop, and then it crashes and I get the error

 trialList=data.importConditions(listInput),
NameError: name 'listInput' is not defined

When I sync the experiment online to pavlovia, same thing, the experiment runs until the experimental loop, then i get the error:

ReferenceError: listInput is not defined

I’ve tried putting the JS code in the Before Experiment, Begin Routine, End Routine tabs of the initializing code routine. I’ve tried adding the code in my experimental loop. The same error every time.
I’ve read on this forum cases of global versus local variable issues in PsychoJS but I haven’t found a solution. Suggestions?
Thank you kindly.

@stelodae6 , you have both Python and JS errors above, so perhaps something is wring with the general logic. Do you have that code nested in a conditional, where perhaps listInput is not set? Please share the code so we can see what is happening.

Hi @dvdbridges
Could you kindly point out the Python and JS errors? The code is written as described, I put it at the beginning of the experiment with other useful js commands.

From the code above, looks like you receive the NameError locally because the Python code is commented out. However, the JS looks ok, do you have any other code in the experiment where you set listInput? You could actually move that code defined in listInput into your trialHandler loop “Conditions” field instead.

@dvdbridges I had commented the Python code out because I was trying to write a working JS code (the Python code was working but the JS code wasn’t).

Thank you for the suggestion about putting the code in the conditions! It works now both locally and online!!

In the “conditions” field I’ve tried various iterations, and both seem to work (the second one causes the resources to load a bit slow at the beginning of the online experiment). I’m sticking with the first one.
1:

$expInfo['participant']+expInfo['GENDER']+'.csv'

2:

$str(expInfo['participant'])+expInfo['GENDER']+'.csv'

I am overjoyed that the experiment is finally working. Thank you @dvdbridges!

1 Like