My experiment stop working properly when I add a code at the start of the main loop to randomize four inner loops!

Hi everybody. I have an experiment consisting of four loops. In each loop there are two code components and and a visual or sound component in different routines. When I run each loop separately, it works well, but when I add a code component at the start of the main loop (consisting of all other four loops) to randomize those four loops, it gets stuck mostly in the second loop. This is my code:

import random
counter=0
L1=[1,0,0,0]
L2=[0,1,0,0]
L3=[0,0,1,0]
L4=[0,0,0,1]
order=[L1,L2,L3,L4]
random.shuffle(order)

And then I add a code component at the end of the loop (inside it) and in the “end routine” tab:

counter+=1

And then in each loop’s “nReps” I write:

$order[counter][0] 
$order[counter][1]
$order[counter][2]
$order[counter][3]

Can anybody tell me what would be the problem?
I really appreciate any help in this.