NameError: name 'trials' is not defined

NameError: name ‘trials’ is not defined
I have run into this error on a trial. Below is some of the code I have.


#Draw the fixation and update the window
text_1.draw()
text_2.draw()
text_3.draw()
text_4.draw()
text_5.draw()
text_6.draw()
fixation.draw()
mywin.update()

core.wait(3.0)
mywin.update()

trials.data.add(‘Accuracy’, accuracy)
trials.data.add(‘Response’, responded[0])

trials.saveAsExcel(fileName=‘data.csv’,
sheetName = ‘rawData’,
stimOut=[],
dataOut=[‘all_raw’])
mywin.update()


I have seen somewhere an asnwer saying you must define in experiment box? What is this?
Any help would be appreciated

Sounds like you haven’t created a trial handler object - or have named it differently. Difficult to say from your code. Can you post the rest?

Hi Oli,

Thank you, thats pointed me the right way. The entire code for my experiment is:

#beginning of code for experiment!
from psychopy import visual, core, event #import some libraries from PsychoPy
trial_timer = core.Clock()

#create a window for the displaying of stimuli
mywin = visual.Window([1920,1080], monitor=“testMonitor”, units=“deg”)

#enter the text for the initial welcome message of the study
text_1 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’On your way to safety you have been captured by the enemy. As a test to see if you are guilty the following test will be adminstered. Press any key when you are ready to continue’,
font=u’Arial’,
pos=(0, 0), height=0.7, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)

#Here I’m going to place the fixation cross for my experiement
fixation = visual.GratingStim(win=mywin, size=0.15, pos=[0,0], sf=0, rgb=-1)

#Draw the fixation cross for subjects to see
#don’t actually need the fixation in yet
text_1.draw()
mywin.update()

#A button press will actually be needed here but that will need sorting later****

#this is the text for the start of the first block, which concerns which city was chosen
text_1 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Our inteligence tells us a certain city has been chosen blah blah blah’,
font=u’Arial’,
pos=(0, 0), height=0.7, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)
text_1.draw()
core.wait(6)
mywin.update()

#button press needed

#pause so can inspect the message

text_1 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Berlin’,
font=u’Arial’,
pos=(9, 9), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)

text_2 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Paris’,
font=u’Arial’,
pos=(17, 0.5), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)

text_3 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Madrid’,
font=u’Arial’,
pos=(9, -7), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)

text_4 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Prague’,
font=u’Arial’,
pos=(-9., -7), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)

text_5 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Nice’,
font=u’Arial’,
pos=(-9, 9), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)

text_6 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Salzberg’,
font=u’Arial’,
pos=(-17., -0.5), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)
fixation = visual.GratingStim(win=mywin, size=0.15, pos=[0,0], sf=0, rgb=-1)

#need a button press here or before???

#Draw the fixation and update the window
text_1.draw()
text_2.draw()
text_3.draw()
text_4.draw()
text_5.draw()
text_6.draw()
fixation.draw()
mywin.update()

event.clearEvents()

core.wait(3) #always wait for 3 sec regardless when response was made
event.getKeys(‘q’‘y’)
mywin.update()

#now a blank screen for a bizzle
core.wait(6.0)
mywin.update()

#draw the stimuli cross
fixation = visual.GratingStim(win=mywin, size=0.15, pos=[0,0], sf=0, rgb=-1)
fixation.draw()
mywin.update()

#Now the second block of test
text_1 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Berlin’,
font=u’Arial’,
pos=(9, 9), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)

text_2 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’London’,
font=u’Arial’,
pos=(17, 0.5), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)

text_3 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Lisbon’,
font=u’Arial’,
pos=(9, -7), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)

text_4 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Brussels’,
font=u’Arial’,
pos=(-9., -7), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)

text_5 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Helsinki’,
font=u’Arial’,
pos=(-9, 9), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)

text_6 = visual.TextStim(win=mywin, name=‘text_4’,
text=u’Zurich’,
font=u’Arial’,
pos=(-17., -0.5), height=0.9, wrapWidth=None, ori=0,
color=u’white’, colorSpace=‘rgb’, opacity=1,
depth=0.0)
fixation = visual.GratingStim(win=mywin, size=0.15, pos=[0,0], sf=0, rgb=-1)

#Draw the fixation and update the window
text_1.draw()
text_2.draw()
text_3.draw()
text_4.draw()
text_5.draw()
text_6.draw()
fixation.draw()
mywin.update()

core.wait(3) #always wait for 3 sec regardless when response was made
event.getKeys(‘q’‘y’)
mywin.update()

END

Thanks

Yeah - trials hasn’t been added - you need to look up trial handlers. Effectively all of your code above would sit in a series of loops which would set your trials. This would be way easier in builder mode though, that will do all the hard work for you.

Also - please include your code between two sets of triple backticks - that way it gets formatted properly