# Show a counter that resets at the beginning of each loop

**URL:** <https://discourse.psychopy.org/t/show-a-counter-that-resets-at-the-beginning-of-each-loop/20158>\
**Category:** Builder\
**Created:** [February 3, 2021, 10:24am UTC](https://discourse.psychopy.org/t/show-a-counter-that-resets-at-the-beginning-of-each-loop/20158 "2021-02-03T10:24:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lcrible](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lcrible/32/6233_2.png) [@lcrible](https://discourse.psychopy.org/u/lcrible)\
**Post date:** [February 3, 2021, 10:24am UTC](https://discourse.psychopy.org/t/show-a-counter-that-resets-at-the-beginning-of-each-loop/20158/1 "2021-02-03T10:24:29Z")

</div>

Hi,  
I have a simple experiment with four identical loops of 12 trials, with only one routine per loop which was copied/pasted so identical for the four loops. I’d like to show a counter in a text that says " 1 out of 12", where the first number updates for each trial.

So far I have created a code component with  
`trialN = 0`  
in Begin experiment and  
`trialN +=1`  
in Begin routine.

In the text component I have` $str(trialN) + ' out of 12'`  
That works except that it keeps adding up in the 2nd, 3rd and 4th loop so that I get 13 out of 12, 14 out of 12 etc.

How can I make it reset to 1 at the beginning of each loop, ideally without having to re-create a different routine in each loop?

Thank you!

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [February 3, 2021, 10:54am UTC](https://discourse.psychopy.org/t/show-a-counter-that-resets-at-the-beginning-of-each-loop/20158/2 "2021-02-03T10:54:15Z")

</div>

Hello Icrible

you could test for the value of trialN and reset it whenever it reaches your limit:

```auto
if trialN == 12:
    trialN = 1
else:
    trialN +=1

```

or you add a routine following your routine in which you reset trialN in a code-element.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![lcrible](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lcrible/32/6233_2.png) [@lcrible](https://discourse.psychopy.org/u/lcrible)\
**Post date:** [February 3, 2021, 11:16am UTC](https://discourse.psychopy.org/t/show-a-counter-that-resets-at-the-beginning-of-each-loop/20158/3 "2021-02-03T11:16:58Z")

</div>

Perfect, thank you @JensBoelte !
