psychopy.org | Reference | Downloads | Github

TypeError: 'NoneType' object is not subscriptable


#1

Hi,

I’m trying to build a short-term memory task with psychopy. The tasks goes as follows:Each word of the target set was presented sequentially for 750 ms, separated from the next word by an interval of 100 ms, with a row of number signs displayed for 400 ms after the third word, and followed by the presentation of a single probe word for 600 ms followed by a blank screen that remained until the subject responded. Participants were instructed to indicate, as quickly as possible without making mistakes, whether the probe word was presented in the last set of three words or not.

Here’s my code:

from constants import *
from psychopy.visual import Window, TextStim, Circle, Rect 
from psychopy.event import waitKeys
from psychopy.core import wait
import random

disp = Window(size=DISPSIZE, units='pix', color=BGC, fullscr=True)


question="Numéro de participant: "
qstim=TextStim(disp,text=question, height=128)
qstim.draw()
respstim=TextStim(disp, text='',height=24)
disp.flip()

response=' '  


while True:
    resplist2=waitKeys(maxWait=float('inf'), keyList=None, timeStamped=True)    
    key, presstime = resplist2[0]
    if len(key)==1:
        response+=key 
    elif key=='space':
        response+=' '
    elif key=='backspace' and len(response)>0:
        response=response[0:-1]
    elif key=='return':
        break

log=open(response+'.tsv','w')

header = ['fixonset', 'mot1', 'mot1onset', 'mot2', 'mot2onset', 'mot3', 'mot3onset', \
'probe', 'probeonset', 'condition' 'response', 'correct', 'RT']

line=map(str, header)
line='\t'.join(line)
line+='\n'
log.write(line)

instructions = "Bienvenue! Voici une tâche de RIP. Si vous êtes prêts, appuyez sur n'importe quelle touche. Gauche pour oui, droite pour non"

inststim = TextStim(disp, text=instructions, color=FGC, height=24)

fixstim2 = TextStim(disp, text=cuemot, color=FGC, height=24, alignHoriz='center', alignVert='center')

blank = TextStim(disp, text='', color=FGC, height=24)

fixstim = Circle(disp, radius=6, edges=32, lineColor=FGC, fillColor=FGC)

allblocs=[]
trialsbloc1={}
trialsbloc2={}

trialsbloc1[0]={}
trialsbloc1[1]={}
trialsbloc1[2]={}

#les essais. Les dict renferment les stimuli de chaque essai
trialsbloc2[0]={}
trialsbloc2[1]={}
trialsbloc2[2]={}


trialsbloc1[0]['mot1']=TextStim(disp, text='abandon', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc1[0]['mot2']=TextStim(disp, text='rejeté', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc1[0]['mot3']=TextStim(disp, text='terreur', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc1[0]['probe']=TextStim(disp, text='rejeté', alignHoriz='center', alignVert='center', height=48, color=FGC)

trialsbloc1[1]['mot1']=TextStim(disp, text='cruel', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc1[1]['mot2']=TextStim(disp, text='seul', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc1[1]['mot3']=TextStim(disp, text='hostile', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc1[1]['probe']=TextStim(disp, text='trahison', alignHoriz='center', alignVert='center', height=48, color=FGC)

trialsbloc1[2]['mot1']=TextStim(disp, text='incompris', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc1[2]['mot2']=TextStim(disp, text='insensible', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc1[2]['mot3']=TextStim(disp, text='méfiance', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc1[2]['probe']=TextStim(disp, text='seul', alignHoriz='center', alignVert='center', height=48, color=FGC)

#bloc2

trialsbloc2[0]['mot1']=TextStim(disp, text='plaisant', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc2[0]['mot2']=TextStim(disp, text='festif', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc2[0]['mot3']=TextStim(disp, text='câlin', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc2[0]['probe']=TextStim(disp, text='festif', alignHoriz='center', alignVert='center', height=48, color=FGC)

trialsbloc2[1]['mot1']=TextStim(disp, text='charme', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc2[1]['mot2']=TextStim(disp, text='créatif', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc2[1]['mot3']=TextStim(disp, text='respect', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc2[1]['probe']=TextStim(disp, text='sincère', alignHoriz='center', alignVert='center', height=48, color=FGC)

trialsbloc2[2]['mot1']=TextStim(disp, text='chanceux', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc2[2]['mot2']=TextStim(disp, text='gaieté', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc2[2]['mot3']=TextStim(disp, text='honnêteté', alignHoriz='center', alignVert='center', height=48, color=FGC)
trialsbloc2[2]['probe']=TextStim(disp, text='créatif', alignHoriz='center', alignVert='center', height=48, color=FGC)


allblocs.append(trialsbloc1)
allblocs.append(trialsbloc2)

fbstim={}


fbstim[0] = TextStim(disp, text='Incorrect!', height=24, color=(1, -1, -1))

fbstim[1] = TextStim(disp, text='Correct!', height=24, color=(-1, 1, -1))

#voici comment randomiser tes blocs
random.shuffle(allblocs)

inststim.draw()
disp.flip()
waitKeys(maxWait=float('inf'), keyList=None, timeStamped=True)

for bloc in allblocs:
    for i in range(3):

        fixstim.draw()
        fixonset=disp.flip()
        wait(FIXTIME)
        bloc[i]['mot1'].draw()
        mot1onset=disp.flip()
        wait(ciblepre)
        bloc[i]['mot2'].draw()
        mot2onset=disp.flip()
        wait(ciblepre)
        bloc[i]['mot3'].draw()
        mot3onset=disp.flip()
        wait(ciblepre)
        fixstim2.draw()
        disp.flip()
        wait(FIXTIME)
        bloc[i]['probe'].draw()
        probeonset=disp.flip()
        resplist=waitKeys(maxWait=float(targetpres), keyList=['left','right'], timeStamped=True)
        response, presstime = resplist[0]
        if response=='left' or response=='right':
            if response=='left' and (bloc[i]['probe']==bloc[i]['mot1'] or bloc[i]['probe']==bloc[i]['mot2'] or bloc[i]['probe']==bloc[i]['mot3']):
                correct=1
            else:
                correct=0
            RT=presstime-probeonset
            disp.flip()
        else:
            blank.draw()
            blankonset=disp.flip()
            resplist = waitKeys(maxWait=float('inf'), keyList=['left','right'], timeStamped=True)
            response, presstime = resplist[0]
            if response=='left' and (bloc[i]['probe']==bloc[i]['mot1'] or bloc[i]['probe']==bloc[i]['mot2'] or bloc[i]['probe']==bloc[i]['mot3']):
                correct=1
            else:
                correct=0
            RT=presstime-probeonset
            if bloc[i]['probe']==bloc[i]['mot1'] or bloc[i]['probe']==bloc[i]['mot2'] or bloc[i]['probe']==bloc[i]['mot3']:
                condition='correct'
            else:
                try:
                    if bloc[i]['probe']==bloc[i-1]['mot1'] or bloc[i]['probe']==bloc[i-1]['mot2'] or bloc[i]['probe']==bloc[i-1]['mot3']:
                        condition='recent'
                    else:
                        condition='incorrect'
                except:
                    condition='incorrect'
        fbstim[correct].draw()
        disp.flip()
        wait(FEEDBACKTIME)
    #bon endroit de log ici!

        line=[fixonset, bloc[i]['mot1'], mot1onset, bloc[i]['mot2'], mot2onset, bloc[i]['mot3'], mot3onset, \
        bloc[i]['probe'], probeonset, condition, response, correct, RT]

        line=map(str,line)
        line='\t'.join(line)
        line+='\n'
        log.write(line)
disp.close()
log.close()

Issue number 1: When I run the code and I wait more than 600 ms to answer, my interpreter tells me that at line 144 (response, presstime = resplist[0]), I have a TypeError: ‘NoneType’ object is not subscriptable. I’m not quite sure to understand what’s the issue. Indeed, presstime is supposed to be an integer. I checked my previous experiences and I noticed that presstime had always been a nonetime, yet my previous experiences worked perfectly. I don’t understand why is isn’t the case this time.

Issue number 2: When I answer quickly, my interpreter telles me that condition has not been defined. Yet, I assigned it to a value earlier…

Can someone enlight me please?

Thank you,

regards.


#2

I actually solved these problems! I made a lot of mistake in this code. Thanks!