| Reference | Downloads | Github

Conditional loop depending on percentage of correct responses


I’m running PsychoPy 1.85.4. on OS High Sierra, using the Builder.

In my experiment, participants see videos and have to rate them as either being shorter or longer (temporal bisection task).
In the training phase, they see some videos, rate them and get direct feedback if they were correct or wrong in their answers. This works fine.
However, I also want to include a conditional looping, where they have to repeat the training session if they had less than a certain percentage of correct answers before continuing with the probe phase.
I looked up the previous topics regarding this task, but the experiment still doesn’t repeat the loop (set the threshold to 95%, still it just goes to the probe phase without repetition)
I designed the experiment as follows


I hope I included all the relevant information. Support would be very much appreciated.

Thank you.

Hi Vau, you should insert some (temporary) debugging code so you can monitor what is happening. e.g. sprinkle some print() statements at appropriate places so you can see how values change from trial to trial:



Lastly, I have no idea why the last bit of code would be comparing a loop to a number (11). That comparison will never be true.

PS please don’t use screenshots of code: it is much easier for us if we can cut and paste from actual text to give useful replies.

Hi Michael

Sure I won’t use screenshots of code anymore…
I forgot to mention that my no. of stimuli is 12, so I did set the no to 11.

…I’ll try that debugging code then.


It’s not the specific number at issue here, but that a loop is not a number, so it can never be equal to a number. A loop is an object which is actually a collection of different things.

In programming languages, we have to be careful to only compare things of the same type
In an analogous fashion in the real world, it makes sense to ask “is this dog a labrador” but not “is this dog the square root of two”?

It might be that you are actually wanting to compare to a specific property of a loop, which is itself a number. e.g. loops have a trial counter, so a valid comparison would be to such a specific property of a loop:

# this is valid, as this specific property of a loop is an an integer:
if training_loop.thisN == 11: 
    # do something

# this is not a valid comparison, as a loop is a complex object called a `TrialHandler`, not an integer:
if training_loop == 11: # will always be false
    # this will never do anything

Hi Michael
this may sound like a stupid question but it’s not clear to me where in the builder do I have to insert the debugging code?
I don’t have prior experience with programming language.

I’m still working with the builder, I set up my whole experiment in it, will still have to make some adjustments afterwards regarding time, files etc. and just wonder if it’s better to switch to the coder for the rest?

Thank you


In the same place where your current custom code is. I can’t cut and paste code from your screen shot to give you a specific example.

Dear Michael
I solved my problem using the following:

  1. Inserting a check routine with a code component:

Begin Experiment:

Begin Routine:
if score/training_loop.nTotal > 0.90:
repeat_training_loop.finished = True
score = 0

  1. Adding a code component in the probe routine

End Routine:
if trainingresp.corr:

Thank you for your help.

1 Like