I want participants to complete 20 random trials among the 150 rows in Excel. As long as I add an excel sheet to the loop, it runs all the rows in the sheet. How can I run 20 trials numbers by adding a code?
Since trials.thisN doesn’t work any more, you could set trialIdx=0 in a Begin Experiment code component tab and then add the following to the End Routine tab.
trialIdx+=1
if trialIdx == 20:
currentLoop.finished=True
If you are running offline, then the most direct and explicit way to do this is to insert a Python expression in the “selected rows” field of your loop dialog:
$randchoice(150, size=20, replace=False)
If you are using an older version of PsychoPy, this function won’t be available to you by default and you will first need to insert a code component (from the “custom” component panel) and put this in the “Begin experiment” tab to give you access to it: