Problem pseudorandimization code - new PsychoPy version

My Experience: I seek to present images of animals, objects, and people to participants to rate valence and arousal on scales of 1-9.

Constraints: I would like a maximum of 3 items of the same category (e.g., 3 animals) to be presented successively. For this, I wrote a pseudorandomization code that I placed in the “BeginRoutine” section.

The problem: This code worked fine on the 2020 version of PsychoPy but since I upgraded to a newer version (2021 or 2022) the experiment no longer launches (a frozen black screen appears) and the problem obviously comes from there (everything is OK when I remove this code). I want to keep the 2022 version, if possible, for the advantages it has in terms of Sliders.

The code used: (sorry for the lack of indentation there)

import random
import pandas as pd
conditionMet = False
while conditionMet == False:
conditions =
for row in range(len(trials)):
conditions.append(trials[row][“stimuliCondition”]) #Name of the conditions type column
for row in range(len(trials) - 2):
if trials[row][“stimuliCondition”] == trials[row + 1][“stimuliCondition”] and trials[row+1][“stimuliCondition”] == trials[row + 2][“stimuliCondition”]:
conditionMet = False
conditionMet = True

df = pd.DataFrame.from_dict(trials)