Question

I am trying to write an Go_nogo Task in Psychopy. Even though I managed to write a script which is working, there are still a few things that make troubles. First, I present pictures of emotional stimuli (im_n, neural; im_a, emotional) and people should only answer by pressing "space" if neutral emotional pictures are presented. When I run the code below everything works well until I don't press any key or the wrong key. So my question is, how do I have to write the code that I don't get kicked out of the run while not answering...? Thanks everybody!

for im in imlist: # Loop for each Image in the List
    picShown = bitmap.setImage(im)
    bitmap.draw()
    win.flip()
    rt_clock.reset()
    resp = False

    while rt_clock.getTime() < timelimit: # timelimit is defined 2 s
         if not resp:
             resp = event.getKeys(keyList=['space'])
             rt = rt_clock.getTime()

    if im in im_n: # im_n is an extra list of one kind of images
         correctResp = 'space'
         if resp[0]==correctResp:
             corrFb.draw() # is defined as a "green O"
         else:
            incorrFb.draw() # is defined as a "red X"

    win.flip()
    core.wait(ISI)

I get the error message:

if resp[0]==correctResp:
IndexError: list index out of range
Was it helpful?

Solution

My guess is that you get this error message at the if resp[0]==correctResp: line:

IndexError: list index out of range

Is that true? If yes, it is simply because event.getKeys() returns an empty list [] if no responses were collected. And doing [][0] will give you the above error because there's no first element (index zero) just like [1,2,3,4][1000] will give you the same error. Note that even if you press a lot of keys and none of them are in the keyList, getKeys will return an empty list because it ignores everything but the contents of the keyList (unless you set keyList=None, in which case it accepts all keys).

There's a few simple ways out of this. Firstly, you can simply check whether resp is empty and give a "fail" score if it is and only check for correctness if it is not. A more general solution, which would work with many response keys and scoring criteria, is to do if correctResp in resp and then score as a success if yes. This comparison will work with an empty list as well, in which case it always returns False as empty lists per definition can't contain anything.

But in your particular case, you only have one response option so it is even simpler! Since you've "filtered" responses sing the keyList, you KNOW that if resp is [], the subject answered "no-go" and conversely if resp is not [], he/she answered "go". So:

if im in im_n:  # im_n is an extra list of one kind of images 
     if resp:  # if subject answered 'go'
         corrFb.draw() # is defined as a "green O"
     else:
        incorrFb.draw() # is defined as a "red X"

Actually, I suspect that you also want to give feedback in trials without neutral images. In that case, define correct as bool(resp) is (im in im_n):

 if bool(resp) is (im in im_n):  # if answer correspond to trial type
     corrFb.draw() # is defined as a "green O"
 else: 
     incorrFb.draw() # is defined as a "red X"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top