Using a for loop to display stimuli from a list

My experiment has two parts. We use participants’ input from Part I to generate the stimuli for Part II.

The outcomes from Part I is a nested list, something like below:

U1 = {"name": "University of Kansas"};
U2 = {"name": "Duke University"};
U3 = {"name": "Kansas State University"};
U4 = {"name": "University of Iowa"};
U5 = {"name": "Purdue University"};
U6 = {"name": "Iowa State University"};
U7 = {"name": "UIUC"};
U8 = {"name": "Penn State University"};
U9 = {"name": "Ohio State University"};
nestedList = [[U1, U2, U3], [U4, U5, U6], [U7, U8, U9]];

In part II, We want to display [U1,U2,U3] as one trial, [U4,U5,U6] as another etc. for nReps trials.

I coded in part II Begin Routine, the Routine is wrapped with Loop (nReps = # lists we generated in Part I)

for triplet in nestedList:
    
    U1text.setText(triplet[0]['name'])
    U1text.draw()

    U2text.setText(triplet[1]['name'])
    U2text.draw()

    U3text.setText(triplet[2]['name'])
    U3text.draw()

U1text, U2text, U3text are three predefined textbox.

The problem I have is it didn’t loop all lists in the nested list, what happened is it always drew the last element in the nest list. In this case,

U7 = {"name": "UIUC"};
U8 = {"name": "Penn State University"};
U9 = {"name": "Ohio State University"};

Gets repeats for nReps. Any help?

It’s going through the entire list-of-lists in BeginRoutine before displaying anything. In other words, that whole loop executes before the routine actually displays, the for loop isn’t connected to the TrialHandler loop at all. There’s no ‘pause’ to display the trial.

It seems like what you want is not to use for triplet in nestedList but instead just

triplet = nestedList[Loop.thisRepN]

and then the rest of your code. Getting the current loop iteration is sometimes a little tricky, but worst-case scenario you can just create a manual loop counter.

Begin experiment:

numReps = 0

Begin routine:

triplet = nestedList[numReps]
numReps += 1

# set the U1/U2/U3 text here the same way you have now.

You also don’t need to call draw() in the “Begin Routine” code, PsychoPy should draw it after that code has executed anyway.

@jonathan.kominsky thank you so much :star_struck: my experiment worked!