Problem in counterbalancing audio files while using spreadsheet

Hi everyone. My experiment includes a excel fine of audio files which has two columns with names of ‘standard’ and ‘deviant’. I wanna present three audio files in each trial: two ‘standard’ and one ‘deviant’ and participants should say which one was different from the other two. There are 30 trials in total and the number of trials in which the deviant file is the first one or second one or third one should be equal, in this case 10.
This is the picture of routines:


This is the code I use for counterbalancing as I said. I’ve put it in the audregcode routine and in the ‘begin routine’ tab.

LIST2=[standard,standard,deviant]
maxRes = 10
seqs = 30

def findDev(l):
   return l.index(davient)

def check(maxRes,l):
   global count1,count2,count3
   shuffle(l)
   d = findDev(l)
   if(d == 0):
       if(count1 < maxRes):
           count1 += 1
           return l
       else:
           return check(maxRes,l)
   elif(d == 1):
       if(count2 < maxRes):
           count2 += 1
           return l
       else:
           return check(maxRes,l)
   else:
       if(count3 < maxRes):
           count3 += 1
           return l
       else:
           return check(maxRes,l)

for i in range(0,seqs):
   check(maxRes,LIST2)

soundVar = 0

in the 'begin experiment tab I’ve defined count1, count2, and count3 all equal to 0.
Then in the sound routine I use

$LIST2[soundVar]

In the ‘end routine’ tab of “sleepaudreg” routine I increase soundVar by 1 and nReps in trials_21 is 3.
If you run the code for counterbalancing you see that it’s working so that 10 times ‘deviant’ is first one, 10 times it’s second one and 10 times it’s third one. But when I run the experiment and look at the data file it’s not equal.

I appreciate it if you could help me in understanding what is wrong here and how I can fix it.
Thank you.