psychopy.org | Reference | Downloads | Github

Adding a variable in the data output file

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Mac OS 10.14.2
PsychoPy version (e.g. 1.84.x): v3.0.0b12
Standard Standalone? (y/n) If not then what?: y

Hello,

I would like to ask for help on generating a column in data output file based on several criteria. Depending on the display type (condition file variable name: display) and the response key pressed, I would like to create a label for each response. For example if the display is named as ’st’ and participant pressed key ‘1’, I would like this to be labelled as global in the data output file in a column named preference. I have 6 displays and 3 response options per display leading to 18 options in total.

I inserted several if-elif statements in the end routine of the code component (please see below) but I got lost with the indentation levels. I also got a syntax error message on “resp.keys_raw” stating that it is an invalid syntax. I named keyboard component as resp and in the output file the relevant column will be resp.keys_raw. So I thought this should be the way to go. Shall I use event.getKeys() instead? If yes how shall I proceed? Or is there a better option?

Thank you very much in advance.

Best,
Ahu

if display==‘st’:
if resp.keys_raw==1
preference = ‘global’
elif:
resp.keys_raw==2
preference = ‘local’
elif:
resp.keys_raw == 1
preference =‘none’
if display == ‘td’:
if resp.keys_raw ==1
preference = ‘local’
elif:
resp.keys_raw == 2
preference = ‘none’
elif:
resp.keys_raw == 3
preference =‘global’
if display == ‘dt’:
if resp.keys_raw ==1
preference = ‘global’
elif:
resp.keys_raw == 2
preference = ‘none’
elif:
resp.keys_raw == 3
preference =‘local’
if display == ‘ds’:
if resp.keys_raw ==1
preference = ‘none’
elif:
resp.keys_raw == 2
preference = ‘global’
elif:
resp.keys_raw == 3
preference =‘local’
if display == ‘ts’:
if resp.keys_raw ==1
preference = ‘local’
elif:
resp.keys_raw == 2
preference = ‘global’
elif:
resp.keys_raw == 3
preference =‘none’
if display == ‘sd’:
if resp.keys_raw ==1
preference = ‘none’
elif:
resp.keys_raw == 2
preference = ‘local’
elif:
resp.keys_raw == 3
preference =‘global’

thisExp.addData(“preference”, preference)

here is the screenshot of the code

Hi @ahugokce, you have some syntax errors in your if statements (indentation errors and missing colons), so try the following

if display == "st":
    if resp.keys_raw == 1:
        preference = "global"
    elif resp.keys_raw == 2:
        preference = "local"
    elif resp.keys_raw == 3
        preference = None
    else:
        pass

Hi David,

Thank you very much for your reply. Things seem to be getting better but I still need some help. The additional column / variable (name: preference) is created but there are no values in that column. In the “begin experiment” tab, I introduced the variable as preference =’ ’ and below is the code I used following your suggestion (None is renamed as neither). Somehow resp.keys_raw did not work so I changed it as resp.keys and it worked.

Thank you very much.
Ahu

Blockquote

if display == “st”:

if resp.keys == 1:

    preference = "global"

elif resp.keys == 2:

    preference = "local"

elif resp.keys == 3:

    preference = "neither"

if display == “td”:

if resp.keys == 1:

    preference = "local"

elif resp.keys == 2:

    preference = "neither"

elif resp.keys == 3:

    preference = "global"

if display == “dt”:

if resp.keys == 1:

    preference = "global"

elif resp.keys == 2:

    preference = "neither"

elif resp.keys == 3:

    preference = "local"

if display == “ds”:

if resp.keys == 1:

    preference = "neither"

elif resp.keys == 2:

    preference = "global"

elif resp.keys == 3:

    preference = "local"

if display == “ts”:

if resp.keys == 1:

    preference = "local"

elif resp.keys == 2:

    preference = "global"

elif resp.keys == 3:

    preference = "neither"

if display == “sd”:

if resp.keys == 1:

    preference = "neither"

elif resp.keys == 2:

    preference = "local"

elif resp.keys == 3:

    preference = "global"

else:

    pass

thisExp.addData(“preference”, preference)

Ok, if resp is your keyboard component, then try adding some print statements to see what is happening when you press 1,2 or 3. So, set the display to “st”, and add print (resp.keys) to the conditional and see what comes out at the end.

Also, since you are using a beta version of PsychoPy (3.0.b12), we recommend that you update PsychoPy to the latest version, if possible, so you can reap the benefits of all the improvements that have been made to the software.

Hi,

Thanks for the alternative suggestions. I updated to the latest version. I am having trouble on where exactly to add the print statement? Below is last version I tried. I tried adding after nested if conditionals / after the first if conditional / before elif… None worked.

Thank you!
Ahu

Blockquote

if display == “st”:

if resp.keys ==1:
    
    preference = "global"
        
elif resp.keys == 2:
    
    preference = "local"

elif resp.keys ==3:
        
    preference = "neither"

else:

    pass

print(resp.keys)

thisExp.addData(“preference”, preference)

I haven’t followed this thread in detail, but the keys returned from the keyboard should be tested for as strings (e.g. '1') instead of integers (e.g. 1).

Thank you @Michael. It worked :slight_smile: