psychopy.org | Reference | Downloads | Github

How to select column in conditions file via code component

Hi,

I’m using Builder on a Mac running Catalina OS.

In a code component, I want to conditionally select one of my columns (to refer to my stim) from my conditions xlsx file. If I was doing this in a Text component, I would just use the $ to access that column but I can’t use this in the code component (I tried this below and it doesn’t work and I know this isn’t correct syntax: left2 = “$left1”).

left1 = None 
left2 = None 
right1 = None 
right2 = None 

if expInfo['Order'] == "A":
    left1 = "WOMAN"
    right1 = "MAN"
    left2 = "$left1"
    right2 = "$right"
elif expInfo['Order'] == "B":
    left1 = "$left"
    right1 = "$right"
    left2 = "MAN"
    right2 = "WOMAN"

How else might I do this? I’m new to PsychoPy and could use some guidance!

1 Like

Hi There,

So each header of your conditions file is a variable name. When you use these variables to set parameters of something in the builder view (e.g. the writing in the text) you need to use the “$” to say you are referring to a variable (in your example “left"). However, when you are referring to variables in code snippits there is no need to say you are looking for a variable (this is assumed when coding), so you can just use the variable/header name without the "” sign (i.e. “left”).

Try this:

left1 = None 
left2 = None 
right1 = None 
right2 = None 

if expInfo['Order'] == "A":
    left1 = "WOMAN"
    right1 = "MAN"
    left2 = left
    right2 = right
elif expInfo['Order'] == "B":
    left1 = left
    right1 = right
    left2 = "MAN"
    right2 = "WOMAN"

note that the only difference from your code is that the “$” is not used.

Hopefully this does the trick :slight_smile:
Becca

1 Like

Hi Becca,

Thanks a lot for your response! I’ve tried this, but unfortunately it doesn’t work. I’m getting this error:

left2 = left
NameError: name 'left' is not defined 

and this is not due to the issues mentioned by Jon here: NameError: name ___ is not defined

Hi There,

This error means that the variable is not defined (and is probably the most common error in python, so good to get accustomed with!).

The usual suspects to check are that the column header of your conditions file is spelt identically to how it it spelt in the code snippet (including any pesky spaces/differences in capitalisation) and that your trials loop has the correct conditions file linked .

Becca

Hi Becca,

Thanks for your reply. Yes, I had doubled checked this before - see the link I posted. It still doesn’t work.