psychopy.org | Reference | Downloads | Github

Stop the loop after 24 (and 96) trials

Good afternoon,

In my experiment, I first need to run a training session (with 24 trials) and then 3 sessions (with 96 trials each). I don’t know how to stop on the 24th and on the 96th trials. I think there is something to do with the trial parameters (because there are many TARLOCS and tarstim, maybe? )

Could someone, please, help me?

Thanks so much,

from psychopy.visual import Window, ImageStim, TextStim
from psychopy.core import wait, quit, logging
from psychopy import gui, visual, core, data, event, logging, clock
from psychopy.event import waitKeys, getKeys
import random
from PIL import Image, ImageDraw

# Create a window
disp = Window (size = (1440, 900), units="pix", fullscr=True) 

#Cue locations
CUELOCS = ["upper","lower"] 

 #Target locations
TARLOCS = ["upcongLeft", "upcongRight","downcongRight"]  

#Potential targets
TARGETS = ["left", "right"] 

#Potential SOAs
SOAS = [0.093, 0.893]

#Fixation time at the start of a trial
FIXTIME = 1.6

#Duration of the cue Screen
CUETIME = 0.1

#Duration of the feedback Screen
FEEDBACKTIME = 1

# number of times to repeat training mode
TRAININGREPEATS = 1

# All possible cues 
cuestim = {}
cuestim["upper"] = ImageStim(disp, size=(300,300), image="upper.png")  
cuestim["lower"] = ImageStim(disp, size=(300,300), image="lower.png")  


#All possible target stimulli
tarstim = {}
tarstim ["upcongLeft"]= ImageStim(disp, size=(300,300), image="upcongLeft.png")
tarstim["upcongLeft"].correctAns    = 'left'
tarstim ["upcongRight"]= ImageStim(disp, size=(300,300),        image="upcongRight.png")
tarstim["upcongRight"].correctAns   = 'right'
tarstim ["downcongRight"]= ImageStim(disp, size=(300,300),     image="downcongRight.png")
tarstim["downcongright"].correctAns  = 'right'

# Create an empty list to contain the training trial
training = []

# LOOP
for cueside in CUELOCS:
    for tarside in TARLOCS:
        for soa in SOAS:
            for tar in TARGETS:
                # Training dictionary
                trialtraining = {"cueside":cueside, "tarside":tarside, "target":tar, "soa":soa}
                # add the trial dictionary to the list
                training.extend (TRAININGREPEATS * [trialtraining])
# Randomise
random.shuffle (training)

Being able to see the indenting of your Python code is really crucial if we are to able to interpret it. Please edit it so we can see where your trial loop is and what it encompasses.

Hi Michael, thanks for your response. I edited the code and let the loop part in the end of it. Thank you

Umm it actually looks like you’ve deleted everything in the trial loop and only left the code where you set up your conditions.

But in essence, if you use enumerate(), you can keep track of your current trial number and do conditional things, like:

for trial_number, trial in enumerate(trials):
    if trial_number == 24:
        break # exit the loop early
    # else carry on with a trial

or you can do things like:

if trial_number % 24 == 0:

to do something on every 24th trial, or

if trial_number in [24, 48 72]:

to do something on specified trial numbers.

1 Like

Hi Michael, thank you for your patience. It worked :slight_smile: