Allowed keys in keybord component will not take variable name (error: variable name not defined)

OS (Win10): **
PsychoPy version (v2021.1.2): **
Standard Standalone? (yes)

What are you trying to achieve?:
I have two types of texts in a variable connected to my conditions file. Either one is randomly presented in each trial iteration followed by a keybord component. In trials where text 1 is presented I want the allowed keys to be different (e.g., 1, 2, 3) from the allowed keys when text 2 is presented (e.g., a,b,c).

What did you try to make it work?:
I have tried to give the name of a variable in the allowed keys field (e.g., posKeys, or just posKeys without ). I have tried to have the variable located in my conditions file like tis:


I have also tried to define a variable in a code component in the beginning of the routine and then change it with an IF statement depending on if the trial is presenting text 1 or text 2, like this:

if (task_text == 1):
posKeys = [‘1’,‘2’,‘3’]
elif (task_text == 2):
posKeys = [‘a’,‘b’,‘c’]

What specifically went wrong when you tried that?:
When I give any variable name the program won’t run and I get this error message:

Traceback (most recent call last):
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment\components\”, line 238, in writeFrameCode
keyList = eval(allowedKeys)
File “”, line 1, in
NameError: name ‘posKeys’ is not defined

I have seen that others have had similar problems, but I have not been able to find a solutions.

Is the parameter set to “each repeat” or to be “constant”? If it’s “constant” then it gets set at the beginning of the experiment - before that variable is created

Thank you for your answer.

The parameter is set to “every repeat” and I have tried to create the variable both before and within the routine. Still the same error message.

That’s strange, could you share the .py file you’re getting with me? It definitely looks like posKeys is being referenced before it’s assigned, so I’d need to see what order it all compiles in (98.8 KB)

Thanks for offering to look at it. Here is the file. Since I can not run the experiment with the variable name in the keyboard component, this file is from a run with these values in the “allowed keys” field: ‘m’,‘f’,‘1’,‘2’,‘3’,‘4’

The variables that I am trying to use are these (I would put $pos_keys in “allowed keys” field):

pos_keys = [‘m’,‘f’]
pos_keys2 = [‘1’,‘2’,‘3’,‘4’]

And I am trying to alter them with this:

if current_target_vis < 1:
pos_keys = pos_keys2

First time the allowed keys input is used (i think )is in line 657.

You’d need to recreate the error for @TParsons to be able to work it out.

However, I think your issue might be that you are using posKeys as both a spreasheet column name and also trying to set it in code. In my experience that gives the kind of error you’re getting.

1 Like

I can’t see this code anywhere in your .py file, is the Code component definitely enabled?

Thank you for your answer. I can recreate the error, but the problem is that the error occurs and aborts the run before the .py file is updated, so I can not provide a script that reflects the error (at least i don’t see how it would be possible).

I have tried having the variable either as a column in my conditions file or as a code component (not double). Now I’m going with the code component, if I can get it to work at all

I have deleted that code and instead I am using this (which should do the same thing):

pos_keys = [‘m’,‘f’]
pos_keys2 = [‘1’,‘2’,‘3’,‘4’]

if current_target_vis < 1:
pos_keys = pos_keys2

(“task_text” corresponds to “current_target_vis” so that if current_target_vis = 1 task_text = 1, and if current_target_vis < 1 task_text = 2.)