# End loop does not work

**URL:** <https://discourse.psychopy.org/t/end-loop-does-not-work/17464>\
**Category:** Builder\
**Created:** [October 22, 2020, 3:27pm UTC](https://discourse.psychopy.org/t/end-loop-does-not-work/17464 "2020-10-22T15:27:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![suzannex](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@suzannex](https://discourse.psychopy.org/u/suzannex)\
**Post date:** [October 22, 2020, 3:27pm UTC](https://discourse.psychopy.org/t/end-loop-does-not-work/17464/1 "2020-10-22T15:27:03Z")

</div>

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

**OS**  
**PsychoPy version** 3.2.4

**What are you trying to achieve?:**  
I am trying to end a loop under a certain condition

**What did you try to make it work?:**  
I used the following if statements (note that time and scotest are defined in the same trial (trial\_2) but a different routine; I putted this routine with code at the end of the trial\_2:

if time ==1 and scotest ==1:  
continueRoutine = False  
fine.finished = True

elif time ==1 and scotest ==0:  
continueRoutine = False  
trials\_2.finished = True

**What specifically went wrong when you tried that?:**  
I checked the output and time and scotest are given correctly. However the trial does not end when I want… I checked the forum and found multiple problems showing the same problem (only mostly when using the online version of Psychopy which I dont use) but I cant figure out how to solve this or what goes wrong…

 ![Screen Shot 2020-10-22 at 5.08.32 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/d/7/d767e0f90893ad759e4de3d6bdcdedab02107253.png)

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [October 22, 2020, 3:30pm UTC](https://discourse.psychopy.org/t/end-loop-does-not-work/17464/2 "2020-10-22T15:30:41Z")

</div>

Hi,

Without a bit more context I can’t be 100% sure, but it looks like you might need to finish your inner loop too in the first if statement?  
i.e.

```auto
if time ==1 and scotest==1:
    continueRoutine = False
    trials_2.finished = True
    fini.finished = True

```

---

<div class="post-metadata">

**Author:** ![suzannex](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@suzannex](https://discourse.psychopy.org/u/suzannex)\
**Post date:** [October 22, 2020, 3:56pm UTC](https://discourse.psychopy.org/t/end-loop-does-not-work/17464/3 "2020-10-22T15:56:56Z")

</div>

Thanks for thinking along!  
Ah this changes something… but now instead of doing nothing (so it improved haha), it only skips the ‘inner’ trail (trials\_2)… The reason I added this outer trial (fini) is because I want participants with a certain score (scotest ==1) and after a certain time (time ==1) to end the experiment and skip a final third trial sequence (that is inside the fini loop, set up after trials\_2). In the first picture you see this third trial part and that this ‘fini’ trial indeed ends before the end screen of the experiment. The code however does not seem to respond to ending this outer fini trial… and now continues to trials\_3 anyway…

Maybe I did something wrong in setting up the fini trial? See picture 2…

 ![Screen Shot 2020-10-22 at 5.51.57 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/9/f/9f19c0994ab179bee0772616b40dffa58e385c3a.png) ![Screen Shot 2020-10-22 at 5.54.48 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/0/6/06f28f195eaa4c8c6f741eb27ad6818ef5d11b5c.png)

Hope I explained it well!

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [October 22, 2020, 4:03pm UTC](https://discourse.psychopy.org/t/end-loop-does-not-work/17464/4 "2020-10-22T16:03:31Z")

</div>

Hi there,

OK so it sounds like you want to also skip all subsequent routines once a score has been obtained, you want to add a small bit of code to the start of the routines that could be skipped.

For example:  
In the ‘begin experiment tab’  
`endTask = 0`

then in your ‘begin routine tab’ (where you already have this):

```auto
if time ==1 and scotest==1:
    continueRoutine = False
    trials_2.finished = True
    fini.finished = True
    endTask = 1

```

then in subsequent routines (that _could_ be skipped in theory) in the ‘begin routine’ tab:

```auto
if endTask:
    continueRoutine = False 
    fini.finished = True

```

Becca

---

<div class="post-metadata">

**Author:** ![suzannex](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@suzannex](https://discourse.psychopy.org/u/suzannex)\
**Post date:** [October 22, 2020, 4:27pm UTC](https://discourse.psychopy.org/t/end-loop-does-not-work/17464/5 "2020-10-22T16:27:45Z")

</div>

Thanks! Yes that is what I want! However, I want to skip routines in another loop (trials\_3) and a separate text block (instruction\_3).

The code I have is in the routine ‘ghh’ part of trials\_2. But how do I skip instruction\_3 and trials\_3? That’s why I thought, let’s make a loop (fini) that covers all trials and can be ended and lead the participant to the ‘end’ routine (i.e. skip instruction\_3 and trilas\_3) in case time and scotest both are 1…

 ![Screen Shot 2020-10-22 at 6.27.24 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/5/e/5edfd5a4e53ce32f7582742136f5e76557ff5a14.png)

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [October 22, 2020, 4:39pm UTC](https://discourse.psychopy.org/t/end-loop-does-not-work/17464/6 "2020-10-22T16:39:39Z")

</div>

Hi OK,

If you want to skip a whole bunch of routines contained in a loop you can set nReps.

Where you have this bit of code:

```auto
if endTask:
    continueRoutine = False 
    fini.finished = True
    trials_3_reps = 0

```

then in your ‘trials\_3’ set nReps to be ‘trials\_3\_reps’

Becca

---

<div class="post-metadata">

**Author:** ![suzannex](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@suzannex](https://discourse.psychopy.org/u/suzannex)\
**Post date:** [October 22, 2020, 5:02pm UTC](https://discourse.psychopy.org/t/end-loop-does-not-work/17464/7 "2020-10-22T17:02:00Z")

</div>

Yes it works, that’s really fantastic, many thanks!!

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [October 22, 2020, 5:04pm UTC](https://discourse.psychopy.org/t/end-loop-does-not-work/17464/8 "2020-10-22T17:04:08Z")

</div>

no worries! please can you mark the solution for future users?

Thanks,  
Becca
