Conditional Randomization in an Excel Spreadsheet

I am running PsychoPy v2023.2.3 on a Mac.

In my experiment, we have a list of decimal pairs in a spreadsheet along with a column to identify if the pair is congruent or not (if it is congruent, the decimal with two digits is larger than the decimal with one digit.) I have this in a loop surrounding the routine currently, with the loopType set to random and the spreadsheet as the condition file.

In the builder, I have two text components, one to display a decimal on the left of the screen, and one to display a decimal on the right of the screen. The participant then is supposed to press the “F” or “J” key to indicate which number is bigger, which I have a keyboard response component for.

I need to write code so that when the rows in the spreadsheet are randomized, no more than three consecutive trials are incongruent or congruent. I have spent some time tinkering around in ChatGPT trying to get a script to work, but I am struggling with getting the code to output something that can be fed into the builder components for display of the stimuli. Below I have attached the code I have been trying to use, but hasn’t been working (I have also run into an issue where PsychoPy says I do not have the right version of openpxl, even though when I run it in my Mac’s terminal, it says I have the fully updated version.)

Begin Experiment:

Begin Routine:

Each Frame:

Any insight would be greatly appreciated!

Have a look at my Randomisation without Repetition online demo for one approach.

Thank you for this resource!

I used that code and it seems to be mostly working, except sometimes I get the following error message:

Based on that error message I think that repeatRows[0] must sometimes be blank so the error happens when you try to evaluate “” + 1. Look for blank rows in your spreadsheet.