| Reference | Downloads | Github

addData string record

Hi there,

I’m currently trying to code for an experiment but I have a few issues. I usually create pretty easy experiments with the builder view, but some code lines are required for the current experiment. Thus, I asked Psychopy to compile the script and then I tried to add a few lines of code where it was needed. I definitely am crap with python language and programming in general, so please forgive me if my questions seem to be stupid.

During the experiment, my subjects will be asked to make a decision between a series of images. In order to do that, they will have to press on the left or right arrow key to go to the next or the previous image, until they press the space bar when they have made their choice, which is then supposed to close the loop. I created a list within a .txt file where I gave a number as name for images (like 1 to 20).

A friend gave me these lines to implement in the script, but I definitely do not master the language. The code lines are the following:

myList = []

with open(‘Height estimation stims list_KaraREV.txt’,‘r’) as myStimFile:
for line in myStimFile:
filepath = line.strip()
myList.append(visual.ImageStim(win, image=filepath)

while True:
keypress = event.waitKeys(keyList=[‘space’,‘left’,‘right’])
key = keypress[0]
if key == ‘left’ and ix>0:
ix = ix-1
elif key == ‘right’ and (ix+1)<len(myList):
ix = ix+1
elif key == ‘space’:

This seems to be working, but my problem is that it doesn’t record anything. I would like it to record, in the data file, the “ix” number, which would refer, if I’m right, to the number of the image where the subject pressed the space bar. I tried to add the code line “thisExp.addData(“name”, value)” with respect to myList[ix], but this is not working (error message: ctypes objects containing pointers cannot be pickled).

The second problem is that, once the space bar has been pressed, it quits the experiment, when I actually just want to go to the next frame of the experiment. I first thought that the core.quit() line caused that problem but I once I deleted it, I had other errors such as the flip had no attribute.

Is there anyone that could help me out with that?

In advance, sorry, my English is not the best, and so are not my programming skills either…

Even for people quite familiar with Python, this is usually a bad idea. Builder scripts are verbose and complicated and it is easy to put code in the wrong place, which makes it very hard for others to make useful suggestions. I’d suggest you go back to the Builder interface, and insert a code component there. Then take the code above and split it across the various tabs in the code component, depending on when you want it to be executed (e.g. at the start of the experiment, at the start of a routine, on every screen refresh, etc). Builder will then take care of inserting the code in the right place, and you can keep working in the graphical environment.

It will then be much easier for us to offer help (because you can show us the code split across the various tabs).

Thanks for the advice Michael! I will follow your suggestion.