VisualStim position randomization - index out of bounds

Hi everyone,

I’m writing the entire experiment in Python 3.6.6., psychopy module version 2020.2.3, on Windows 10.
The experiment consists in showing one target word and 4 choices on the screen, simultaneously.
The positions of the 4 choices has to be randomized from one trial to another so that people don’t get used to having a correct answer in the same position.

Here’s what I tried to do:

positions = [[0,100],[0,0],[0,-100],[0,-200]] #define positions
positionsRand = rn.sample(positions,len(positions)) #randomize positions

for trial in test_words: #this loops over target words
        
    tword = num_stimuliRand['test_novel_word'][trial] #pick a target word
    
    showStim = visual.TextStim(win, text=tword, color='red', pos=[0,250], ori=0, font='Arial', height=25, bold=True, wrapWidth=1720)

    for numP, p in enumerate(positionsRand):
              
        curr_pos.append(p)

    #for the 4 choices, in pos I put elements of curr_pos list in order they appear in the list
    button1 = visual.TextStim(win, text=num_stimuliRand['ans_correct'][trial], color=[.8,.8,.8], pos=[curr_pos[0]], ori=0, font='Arial', height=25, wrapWidth=1720)
    box1 = visual.Rect(win, fillColor = 'yellow', lineColor = 'blue', width = 70, height = 30,pos=[curr_pos[0]])
    button2 = visual.TextStim(win, text=num_stimuliRand['ans_incompatible'][trial], color=[.8,.8,.8], pos=[curr_pos[1]], ori=0, font='Arial', height=25, wrapWidth=1720)
    box2 = visual.Rect(win, fillColor = 'yellow', lineColor = 'blue', width = 70, height = 30,pos=[curr_pos[1]])
    button3 = visual.TextStim(win, text=num_stimuliRand['ans_overarching_cat'][trial], color=[.8,.8,.8], pos=[curr_pos[2]], ori=0, font='Arial', height=25, wrapWidth=1720)
    box3 = visual.Rect(win, fillColor = 'yellow', lineColor = 'blue', width = 70, height = 30,pos=[curr_pos[2]])
    button4 = visual.TextStim(win, text= num_stimuliRand['ans_gramm_incompatible'][trial], color=[.8,.8,.8], pos=[curr_pos[3]], ori=0, font='Arial', height=25, wrapWidth=1720)
    box4 = visual.Rect(win, fillColor = 'yellow', lineColor = 'blue', width = 70, height = 30,pos=[curr_pos[3]])

    showStim.draw(win=win)  
    button1.draw(win=win) 
    box1.draw()
    button2.draw(win=win)  
    box2.draw()
    button3.draw(win=win)
    box3.draw()
    button4.draw(win=win)
    box4.draw()         
    event.waitKeys(keyList=['space'])
    win.flip()
    core.wait(0.2)

It shows me this error message:
Traceback (most recent call last):

File “”, line 130, in
button1.draw(win=win)

File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\text.py”, line 806, in draw
GL.glTranslatef(self.posPix[0], self.posPix[1], 0)

IndexError: index 1 is out of bounds for axis 0 with size 1

I don’t understand what it means?

Hi There,

Means that you are trying to get extract the second element from a list but there are not two elements in the list.

To reproduce the error type the following into the ‘shell’ section of coder view:

a = ['first thing']
a[1]

and to solve it type:

a=['first thing', 'second thing']
a[1]

In your case it looks like the position of button 1 might not be what you expect it to be. Try adding a few print statements to check this, add print(button1.pos) on the line after where you set button1.

Hope this helps,
Becca

Hi Becca,

thanks, I managed to fix it. The problem was in double brackets in “pos=[curr_pos[0]]”, it has to be “pos=curr_pos[0]”.