Updating Variable Count in Loop

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): MacOS Big Sur
PsychoPy version (e.g. 1.84.x): 2020.2.10

What are you trying to achieve?: I have a conditional loop set up containing two routines, the first where the participant types freely and the second one which contains an image and sound that serve as a reinforcer. The second routine is only displayed if a certain percentage of the words the participant typed are longer than a certain number of characters. This uses a loop that has nreps defined by nextRoutineNreps. In the first routine, I would like to display a count that shows how many times the second conditional routine has played (ie. how many times they reached the criterion necessary for reinforcement).

What did you try to make it work?: I tried using a display text component that refers to a variable hcount. I set hcount as 0 in the begin experiment tab of a code component in the first routine and used the following code in the end routine tab of another code component in the second routine:

hcount = 0

if trialloopname.thisN == 1:
hcount = 1

What specifically went wrong when you tried that?: However, the count display simply remained at 0 when the experiment looped back to the first routine after a successful criterion


I am not quite sure if I understood what you want to achieve but the attached PsychoPy-Exp displays the loop iterations.thistrial.psyexp (6.4 KB)

It not clear for me when you reset hcount and why you use the if-construction. That doesn’t seem to fit your description. Screen-shots of the relevant routines might help.

Best wishes Jens

Hi Jens,

This is close to what I would like to do, except I have two routines within one loop, and the second routine within its own conditional loop. I want the first routine to display the number of iterations of the second loop. I have attached a screenshot of my experiment for reference. Thank you!


you mean like this thistrial.psyexp (7.1 KB) ?

Best wishes Jens


I just wanted to add what I did to solve this problem in case anyone has a similar issue. I first set up the loop so that the second routine present_reinforcement would only be presented if the participant met my criteria of a certain average length of words typed. To do this, I put a loop around the reinforcement routine and set the nReps to nextRoutineNreps. When the participant met the criteria, I set nextRoutineNreps to 1, and when they did not, I set it to 0. After this, I created a variable ‘hcount’ that is set at 0 at the beginning of the experiment and is increased by one every time that criteria is met. I then used a text component and entered $hcount in the text field!