# Naming Error when Trying to do Counterbalancing/Branched experiment

**URL:** <https://discourse.psychopy.org/t/naming-error-when-trying-to-do-counterbalancing-branched-experiment/27947>\
**Category:** Builder\
**Created:** [March 12, 2022, 3:04pm UTC](https://discourse.psychopy.org/t/naming-error-when-trying-to-do-counterbalancing-branched-experiment/27947 "2022-03-12T15:04:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aleya\_Marzuki](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/aleya_marzuki/32/33338_2.png) [@Aleya\_Marzuki](https://discourse.psychopy.org/u/Aleya_Marzuki)\
**Post date:** [March 12, 2022, 3:04pm UTC](https://discourse.psychopy.org/t/naming-error-when-trying-to-do-counterbalancing-branched-experiment/27947/1 "2022-03-12T15:04:29Z")

</div>

**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](https://www.youtube.com/watch?v=45NRWjFiYK0)

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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/3/83df7da92080fd867f2121f75612501ba1f9f3b6.png)

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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/9/19755b0d6d544d7cffdee9126e38d2855deafa8d.png)

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [March 13, 2022, 9:15am UTC](https://discourse.psychopy.org/t/naming-error-when-trying-to-do-counterbalancing-branched-experiment/27947/2 "2022-03-13T09:15:51Z")

</div>

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.

```auto
nReps_A = 0
nReps_B = 0

```

Thanks,  
Becca

---

<div class="post-metadata">

**Author:** ![Aleya\_Marzuki](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/aleya_marzuki/32/33338_2.png) [@Aleya\_Marzuki](https://discourse.psychopy.org/u/Aleya_Marzuki)\
**Post date:** [March 14, 2022, 2:01am UTC](https://discourse.psychopy.org/t/naming-error-when-trying-to-do-counterbalancing-branched-experiment/27947/3 "2022-03-14T02:01:24Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/2/9/2922ae0fede23994d7d3d850371b885d7155f575.png)

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!

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [March 14, 2022, 8:03am UTC](https://discourse.psychopy.org/t/naming-error-when-trying-to-do-counterbalancing-branched-experiment/27947/4 "2022-03-14T08:03:24Z")

</div>

Put the 1 and 2 in quotes. Keys are strings

---

<div class="post-metadata">

**Author:** ![Aleya\_Marzuki](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/aleya_marzuki/32/33338_2.png) [@Aleya\_Marzuki](https://discourse.psychopy.org/u/Aleya_Marzuki)\
**Post date:** [March 14, 2022, 8:59am UTC](https://discourse.psychopy.org/t/naming-error-when-trying-to-do-counterbalancing-branched-experiment/27947/5 "2022-03-14T08:59:38Z")

</div>

This worked, thank you!
