# End Loop based on the percentage of correct responses

**URL:** <https://discourse.psychopy.org/t/end-loop-based-on-the-percentage-of-correct-responses/18065>\
**Category:** Builder\
**Created:** [November 12, 2020, 12:55pm UTC](https://discourse.psychopy.org/t/end-loop-based-on-the-percentage-of-correct-responses/18065 "2020-11-12T12:55:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alonso-Vega](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alonso-vega/32/11776_2.png) [@Alonso-Vega](https://discourse.psychopy.org/u/Alonso-Vega)\
**Post date:** [November 12, 2020, 12:55pm UTC](https://discourse.psychopy.org/t/end-loop-based-on-the-percentage-of-correct-responses/18065/1 "2020-11-12T12:55:31Z")

</div>

**OS** : Win10  
**PsychoPy version** :2020.2.5  
**Standard Standalone?** Yes  
**What are you trying to achieve?:**  
To end a loop based on the percentage of correct responses after some trials.

It’s my first time with psychopy and I know if i use this code the routine stops after 3 correct responses:

score+=key\_resp\_2.corr  
thisExp.addData(‘Score’,score)  
if score \>= 3:  
trial.finished = True

But I don’t know how to code to end the loop with 95% correct responses of at least 25 trials.

Thanks!

---

<div class="post-metadata">

**Author:** ![TParsons](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tparsons/32/8301_2.png) [@TParsons](https://discourse.psychopy.org/u/TParsons)\
**Post date:** [November 12, 2020, 2:00pm UTC](https://discourse.psychopy.org/t/end-loop-based-on-the-percentage-of-correct-responses/18065/2 "2020-11-12T14:00:27Z")

</div>

The code you have adds `1` to `score` if a correct response is given, to continuously work out an average I would recommend instead _appending_ `1` to `score`. So rather than:

```python
score = 0
score += key_resp_2.corr

```

you would do

```python
score = []
score.append(key_resp_2.corr)

```

what this means is that `score` becomes a list of `1`s and `0`s, which you can then work out an average for and compare this average to 0.95 (which it will be if it’s 95% correct):

```python
if average(score) >= 0.95:
    trial.finished = True

```

A word of caution: You might want to start off with a few `0`s in `score` rather than have it blank, otherwise they will get one correct answer and the task will finish because their correct % will be 100%. Either that or don’t calculate average until a few repetitions in.

---

<div class="post-metadata">

**Author:** ![Alonso-Vega](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alonso-vega/32/11776_2.png) [@Alonso-Vega](https://discourse.psychopy.org/u/Alonso-Vega)\
**Post date:** [November 12, 2020, 6:01pm UTC](https://discourse.psychopy.org/t/end-loop-based-on-the-percentage-of-correct-responses/18065/3 "2020-11-12T18:01:08Z")

</div>

> [@TParsons](#):
>
> `score.append`

Thanks! I tried and it works perfectly.

Also, I solved the problem of the posible and unexpected finish of the experiment with just one correct answer adding a minimun of correct answers required. Before experiment:

score =   
scorr = 0

End Routine:  
score.append(key\_resp\_2.corr)  
scorr+=key\_resp\_2.corr  
thisExp.addData(‘Score’,score)  
if (average(score) \>= 0.95 and scorr \>= 10):  
trial.finished = True

Thanks!
