Naming Error when Trying to do Counterbalancing/Branched experiment

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): v2022.1.1
Standard Standalone? (y/n) Y
What are you trying to achieve?: I’ve just started tinkering around with Psychopy today and I want to construct a task with two different counterbalancing conditions. I want participants to only see one of the conditions based on keyboard input by the experimenter. I’m using this tutorial released by Psychopy as basis: How to Make Branched Experiments in PsychoPy: An Example using Consent forms - YouTube

What did you try to make it work?:
I have two conditions placed in separate loops.

In the showCounterbalance properties for both conditions I have assigned the nreps option to be a variable:

Prior to these conditions, I have a routine where I ask for input, whether 1 or 2 depending on which counterbalancing condition is wanted.

I then use this input to assign values to the nreps variables in code

if counterbalance_input.keys == 1:
  nReps_A = 1
  nReps_B = 0
  
elif counterbalance_input.keys == 2:
  nReps_A = 0
  nReps_B = 1

The code should assign one of the ‘nReps’ to be 0, thereby suppressing the corresponding condition.

What specifically went wrong when you tried that?:

I’m getting a naming error:

showCounterbalance1 = data.TrialHandler(nReps=nReps_A, method=‘random’,
NameError: name ‘nReps_A’ is not defined

I’ve checked many times through that the names are consistent in the code and in the properties section. Any insight into what might be happening? Thank you.

Which tab of the code component have you put your code in? Does it work if you provide some “initialization” of the variable in the begin experiment tab?

e.g.

nReps_A = 0
nReps_B = 0

Thanks,
Becca

Hi Becca,

I have a routine called ‘Counterbalancer’ right at the start of the experiment where I’ve put the code in and I put the code itself into the End Routine tab.

I’ve tried your suggestion, typing nReps_A = 0 and nReps_B = 0 in the Begin Experiment tab, but all that does now is assign both variables to be 0 and neither condition runs when I start the experiment. Psychopy does not seem to be taking the input from the End Routine code. Is there anything else I could try? Thank you!

Put the 1 and 2 in quotes. Keys are strings

This worked, thank you!