| Reference | Downloads | Github

Conditionally repeat trial in touchscreen task



Hi all,

Back again! So I’m working my way through a number of issues I bump into with my task which I’ve been posting about on here. To give some overview, I’m using a Windows 10 laptop/tablet hybrid. I’ve created a touchscreen task to be used with children where they tab on different stim to give their answer. Because of the multitouch aspect of my screen, I had to use the code component provided by Michael in this thread to establish when stim was tapped on:

Now I need a way to keep repeating a trial until a participant gives the correct answer. I also need a way of playing conditional audio files dependent on whether or not a correct or incorrect answer was given.

So to clarify:

  • Incorrect answer given. Play “incorrect answer” audio. Repeat this trial.
  • Correct answer given. Play “correct answer” audio. Move onto the next trial.

I’ve already got the trial ending only on a “correct” answer tap but have been failing to proceed with the rest of this.

I’m still pretty new to PsychoPy and coding languages so any advice would be appreciated :slight_smile:


OK, you need to surround that trial with another loop (i.e. nested inside your main trial loop, only encompassing this particular trial, and not linked to a conditions file). Give that inner loop a relatively high nRep value, which is higher than any likely number of failed trials (e.g. 99). Uncheck the is trials setting on that loop dialog box (as effectively this loop is running within your main trial structure, presumably controlled via one repetition of your outer loop). If you don’t do this, the data output structure will be very confusingly structured (i.e. you won’t get one row per trial, but some variable number).

A successful click will terminate the current routine as well as end the loop. An incorrect click will just end the trial (sending the participant back to another repetition of that trial). On a correct response, you should also record the iteration number of that inner loop, so you know how many incorrect responses there were before the eventual correct one (will be 0 if there is a correct response on the first iteration):

# run on every frame:
for stimulus in stimuli:
    if stimulus.contains(mouse): 
        if == corrAns: # correct response
            thisExp.addData('n_incorrect_responses', your_inner_loop_name.thisN)
            continueRoutine = False
            your_inner_loop_name.finished = True
        else: # incorrect response
            continueRoutine = False
        break # stop checking after the first detected stimulus (in case of overlaps)


Hi Michael,

Thanks for your quick response. I’ve integrated your suggestion (tried something similar myself earlier but not as neatly!) but the issue is that the whole experiment just ends regardless of whether I give an incorrect or correct response. I’m not receiving any errors though.

I’ve attached some printscreens just in case there’s something glaringly obvious I’m doing wrong (temporarily leaving the audio out until the conditional ending is fixed).

Thanks again!

  • You don’t have the mouse component set to end the routine, do you? That could conflict with the custom code.
  • We probably need to see the dialog box settings for the inner loop.
  • It’s not clear what you mean by " the whole experiment just ends". Do you mean that only one trial is ever presented?
  • perhaps insert some debugging code, like: