Loop variable referenced before assignment

I have a loop called “practice_trials” with 7 parameters. I am having trouble figuring out how to access “compQ”, which is another column in this loop. The following code results in the error: local variable compQ referenced before assignment.

In the Begin Routine tab:
if compQ == ‘x’:
continueRoutine = False; # no comprehension Q
else:
prac_quest_text.text = compQ; # set compQ
continueRoutine = True;

  • I tried changing the order of the objects in this routine, putting prac_quest_text below the code block and above it.
  • When I click on the practice_trials loop I can see that compQ is being read in as a parameter.

I tried setting compQ explicitly like so:
compQ = practice_trials.thisTrial[‘compQ’]

This makes the “compQ referenced before assignment” error go away, and when I print(compQ) the correct string is printed. But why is this happening?

What’s really puzzling is that in the routine just after this one, the exact same logic is used to set a text component and skip a routine:

In the Begin Routine tab:

if feedb == ‘x’:
continueRoutine = False;
else:
prac_feedback_text.text = feedb;
continueRoutine = True;

And this works perfectly!! There is no difference except that for these trials, feedb will never equal ‘x’.

OS (e.g. Win10): Win11
PsychoPy version (e.g. 2024.2.4 Py 3.8): 2024.1.5
Standard Standalone Installation? (y/n) y
Do you want it to also run online? (y/n) yes eventually, but just want it to run locally for now

Are you using compQ for something else as well?

I use compQ later in the experiment for the same thing (setting comprehension question text and skipping comprehension text, based on a column in the conditions spreadhseet). It is in a separate loop in a Begin Routine tab.

That should be fine.

Do you have any parameters using spreadsheet variables set to constant?

The text object that is being set = compQ is constant in one routine and set every repeat in another. I did mess around with this but it didn’t seem to change anything about the order of compQ assignment.

I think the most suspicious thing in this experiment is that in a “Begin Experiment” code tab I load conditions with data.importConditions(currList) and make a pseudorandom order, and this code works fine but the auto->JS says there is a syntax error.

This means that compQ needs to have a value set in a Begin Experiment code component which appears earlier in your experiment. Online this will fail because you can’t set/alter variables in code components that are also set by spreadsheets.

If you use code to load a spreadsheet then you can’t just use the column names to access the contents.