Presenting routine once, randomly, within a loop of X trials

In each block of X trials, I would like to randomly present a sequence of two routines once.

Since there is no way of doing this through the interface, I have to do this with a python script.
So I made a loop for the block that contains the loop for all trials as well as the routine sequence.

So I need a variable that stores whether the sequence has been run already and choose a number between 1 & X that will start the sequence. For each iteration of the trial, I need the script to randomly select a number between 1 & X, which has not been selected before. After the sequence, I need to go back to the loop of trials & skip the sequence before moving on to the next block.
I have no clue how to write python. Can you point me to how to a resource to learn how to do this?

I hope someone can help!

Are you using TrialHandler for your main loop of X trials? Something like this could work:

uniqueStimNames = ['a', 'b', 'c', 'd', 'e'] # X = 5 in this example
stimList = []
for stim in uniqueStimNames:
    stimList.append({'stimname':stim})

nReps = 10

normalTrials = data.TrialHandler(stimList, nReps) 

# create a virtual deck of X+1 cards, where we will draw one on each loop to see 
# if we should do a regular trial or run the bonus routine
oneSetOfTrials = ['normal trial']*len(uniqueStimNames) + ['bonus routine']*1

totalNtrials = len(oneSetOfTrials)*nReps

# start the main experiment loop
for thisTrialN in range(totalNtrials):
    
    # check where we are in the current set of X+1
    numberInCurrentSet = thisTrialN % len(oneSetOfTrials)
    # if we're at the beginning of a new set
    if numberInCurrentSet == 0:
        # shuffle the X normal trials and 1 bonus routine into a new random order
        random.shuffle(oneSetOfTrials)
    
    if oneSetOfTrials[numberInCurrentSet] == 'normal trial':

        thisTrial = next(normalTrials)

        # fill in your code to do the regular thing

    elif oneSetOfTrials[numberInCurrentSet] == 'bonus routine':

        # fill in your code for the bonus routine