# Selecting one of the loops conditionally

**URL:** <https://discourse.psychopy.org/t/selecting-one-of-the-loops-conditionally/2210>\
**Category:** Builder\
**Created:** [March 28, 2017, 3:21pm UTC](https://discourse.psychopy.org/t/selecting-one-of-the-loops-conditionally/2210 "2017-03-28T15:21:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuppuslp](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kuppuslp](https://discourse.psychopy.org/u/kuppuslp)\
**Post date:** [March 28, 2017, 3:21pm UTC](https://discourse.psychopy.org/t/selecting-one-of-the-loops-conditionally/2210/1 "2017-03-28T15:21:22Z")

</div>

Dear all  
I use psychopy builder. The task is simple, participant hears a sentence and need to select one of the images on screen. Depends on the response the variables such as scores, incorr1, wrongAnswer are saved.  
This part of the program runs perfectly.

![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/f/f043bd33b3312d173607c2b02747061dce2ab68b.PNG)

 ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/f/fe0965631265e988675be44353058ea43202b91b.PNG)

 ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/4/484910ca06c168fa5bbb674dcce7413c201ebb51.PNG)

As you can see I got three loops. I want to run either loop 2 (trials\_3) or loop 3 (trials\_2) based on one of the conditions which is met at the end of loop 1. I have included that in my code component (each frame) towards the end (the last four lines).

I struggled to make this happen. I understand that I need to do something with the last four lines of the code in the each frame section to do this.

I also understand that I need to define the loops upfront in beginning of the experiment. I am new to psychopy and any help is appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [March 28, 2017, 9:27pm UTC](https://discourse.psychopy.org/t/selecting-one-of-the-loops-conditionally/2210/2 "2017-03-28T21:27:02Z")

</div>

Set some variables something like this:

```auto
if some_test:
    loop_2_reps = 10
    loop_3_reps = 0
else:
    loop_2_reps = 0
    loop_3_reps = 10

```

The put those variables (e.g. `$loop_2_reps`) in the nReps fields of their respective loop dialogs. Loops with an nReps of 0 won’t run.

---

<div class="post-metadata">

**Author:** ![kuppuslp](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kuppuslp](https://discourse.psychopy.org/u/kuppuslp)\
**Post date:** [March 29, 2017, 8:59am UTC](https://discourse.psychopy.org/t/selecting-one-of-the-loops-conditionally/2210/3 "2017-03-29T08:59:58Z")

</div>

Thanks Michael

I added this in my Each Frame section after the condition I mentioned, and it seems to work perfectly now.

Thanks a lot. This is how it looks at the moment.

for stimulus in [image, image\_2, image\_3]:

```
    # check if the mouse is pressed within the current one:
    if mouse.isPressedIn(stimulus):

        # Yes, so store the reaction time in the data:

        # check if the stimulus' image filename matches the correct answer:
        if stimulus.image == eval(corrAns):
             thisExp.addData('score', 2)
             score = score + 2
        elif stimulus.image == eval(clCorrAns):
             thisExp.addData('score', 1)
             score = score + 1
             thisExp.addData('incorr1', 1)
             incorr1= incorr1 + 1
        elif stimulus.image == eval(incorr):
             thisExp.addData('wrongAnswer', 1)
             wrongAnswer = wrongAnswer + 1
        # end the trial once done here:
        continueRoutine = False 
      # trials_5.finished = True
        if incorr1 >= 1 or wrongAnswer >= 1:
            loop_2_reps = 1
            loop_3_reps = 0
        elif score == 4:
            loop_2_reps = 0
            loop_3_reps = 1
        break
```

---

<div class="post-metadata">

**Author:** ![kuppuslp](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kuppuslp](https://discourse.psychopy.org/u/kuppuslp)\
**Post date:** [March 29, 2017, 12:15pm UTC](https://discourse.psychopy.org/t/selecting-one-of-the-loops-conditionally/2210/4 "2017-03-29T12:15:28Z")

</div>

Hi Michael

Just another little tweak required, which I am not able to get done.

I need to add ‘+8’ to the score (I want it in the same column as ‘score’ in data sheet), if the loop\_3\_reps was run. Else, If the loop\_2\_reps was run, it would like the score to just be unaltered.

![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/9/95ea6bd48dae5056921aea27862bec4b18af33be.PNG)

 ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/0/0825c06da656befe88cc1e6f1019fb96c0befc25.PNG)  
 ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/4/485bfb90e4ecb68647277f7a517e815c77d4f172.PNG) 

Any help is appreciated.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [March 29, 2017, 9:09pm UTC](https://discourse.psychopy.org/t/selecting-one-of-the-loops-conditionally/2210/5 "2017-03-29T21:09:50Z")

</div>

I guess you could just add 8 to the score in the same section of code where you set the repetition number for loop 3 to be 1.

---

<div class="post-metadata">

**Author:** ![kuppuslp](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kuppuslp](https://discourse.psychopy.org/u/kuppuslp)\
**Post date:** [March 30, 2017, 9:21am UTC](https://discourse.psychopy.org/t/selecting-one-of-the-loops-conditionally/2210/6 "2017-03-30T09:21:46Z")

</div>

Hi Michael

I added something like this, and it works. Thanks

```
        if incorr1 >= 1 or wrongAnswer >= 1:
            loop_2_reps = 1
            loop_3_reps = 0
        elif score == 4:
            loop_2_reps = 0
            loop_3_reps = 1
            thisExp.addData('score', 10)
            break
```
