# Multiple conditions needed for running a loop

**URL:** <https://discourse.psychopy.org/t/multiple-conditions-needed-for-running-a-loop/13095>\
**Category:** Builder\
**Created:** [May 6, 2020, 7:26pm UTC](https://discourse.psychopy.org/t/multiple-conditions-needed-for-running-a-loop/13095 "2020-05-06T19:26:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eslifkin](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@Eslifkin](https://discourse.psychopy.org/u/Eslifkin)\
**Post date:** [May 6, 2020, 7:26pm UTC](https://discourse.psychopy.org/t/multiple-conditions-needed-for-running-a-loop/13095/1 "2020-05-06T19:26:05Z")

</div>

Experiment flow for reference:

 ![1](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/5/9/5970a0ebe1f2211b34b24373aafdfe5733e4457b.jpeg)

The Popup\_Loop is a conditional loop (it only runs if a variable equals 1 on a trial) and everything about it works fine. On the “Email” routine, I have a keyboard response after an image is shown, and then a code component that evaluates if the Popup\_Loop should run. This is what that code looks like currently:

 ![3](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/2/e/2e56138dddbbf382dfc9a764fa3288a28dd6d816.jpeg)

I need to add to that code additional conditions for running the Popup\_Loop. What I need to happen is that on trials where Popup\_Presence=1, if participants hit a key to respond to the picture, or if 2 seconds passes with no response, the Popup\_Loop needs to be skipped.  
On trials where Popup\_Presence=0, participants need to be able to hit a key at any time to end the routine.

Basically what I’m trying to achieve is the following:

1. If Popup\_Presence=1 and a key is hit within 2 seconds, don’t run Popup\_Loop
2. If Popup\_Presence=1 and 2 seconds passes with no response, run Popup\_Loop immediately
3. If Popup\_Presence=0, skip Popup\_Loop and allow response at any time (response will then end the routine)

I don’t know the proper Python “terms” to make this code component  
Any help is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![Eslifkin](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@Eslifkin](https://discourse.psychopy.org/u/Eslifkin)\
**Post date:** [May 6, 2020, 8:49pm UTC](https://discourse.psychopy.org/t/multiple-conditions-needed-for-running-a-loop/13095/2 "2020-05-06T20:49:12Z")

</div>

Update:  
I have been working on this, and so far no luck still. This is the most recent thing I’ve tried:

 ![1](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/e/8e21a4f7dfb3a1c673b14d2b88095cd9a1644923.jpeg)

I have a feeling I’m using the wrong thing for my keyboard variable. My keyboard response is called ‘Email\_Response’. I also don’t know if “None” is the proper term for no response.

I also don’t know if I’m doing the time check correctly.

---

<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:** [May 7, 2020, 2:12pm UTC](https://discourse.psychopy.org/t/multiple-conditions-needed-for-running-a-loop/13095/3 "2020-05-07T14:12:18Z")

</div>

With this level of complexity I’d advise starting with pseudo-code! I think you have `if`s and `elif`s contradicting one another; if ‘Popup\_presence’ is 1, then the `elif` won’t run, so having `Popup_presence == 1` as a condition for the `elif` means it’ll never run.

With regard to checking whether there has been a response, a cool aspect of Python is that if you supply a non-boolean argument to an if loop, it will convert to boolean based on whether or not it is empty. So `if Email_response.keys` is essentially the same as `if Email_response.keys == []` or `if len(Email_response.keys) == 0`. If there’s not been a response, then `Email_response.keys` will be empty.

Based on what you wrote, you’d want something like this in `Each Frame`:

```auto
if Popup_Presence==1 and t >= 2 and not Email_response.keys: # If Popup_Presence is 1 and 2 seconds pass with no response...
    # Run Popup_Loop immediately
elif Popup_Presence==1 and Email_response.keys: # If Popup_Presence is 1 and a key is hit within 2 seconds...
    # Don't run Popup_Loop
elif Popup_Presence==0 # If Popup_Presence is 0...
    # Skip Popup_Loop...
    if Email_response.keys # ...and allow response at any time, a response will then...
        # End the routine

```

So filling in the loops with actions now, you seem to already have most of what you need. Like in your example, you could do:

```auto
if Popup_Presence==1 and t >= 2 and not Email_response.keys: # If Popup_Presence is 1 and 2 seconds pass with no response...
    nRepsPopupLoop = 1 # Run Popup_Loop... 
    continueRoutine = False # ...immediately
elif Popup_Presence==1 and Email_response.keys: # If Popup_Presence is 1 and a key is hit within 2 seconds...
    nRepsPopupLoop = 0 # Don't run Popup_Loop
elif Popup_Presence==0 # If Popup_Presence is 0...
    nRepsPopupLoop = 0 # Skip Popup_Loop...
    if Email_response.keys # ...and allow response at any time, a response will then...
        continueRoutine = False # End the routine

```

Is this pretty much what you want it to do? Jon pointed out something else helpful this morning - anything which involves crashing out of a loop may not work directly in JavaScript just due to differences in the languages. Rather than having it set to `Auto-JS` you may be better off coding in `both` and then making alterations to the JS script. I’m mostly involved with the Python side of things but I think @apitiot has added a JS function which can end a loop safely.

---

<div class="post-metadata">

**Author:** ![Eslifkin](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@Eslifkin](https://discourse.psychopy.org/u/Eslifkin)\
**Post date:** [May 7, 2020, 3:40pm UTC](https://discourse.psychopy.org/t/multiple-conditions-needed-for-running-a-loop/13095/4 "2020-05-07T15:40:26Z")

</div>

Thank you so much!! With 0 coding experience, I was thinking I was somewhere on the right track, but had no idea how to get to where I needed to go. I think what you detailed is exactly what I need to happen. I assume this needs to be in the ‘each frame’ section since it deals with time, is that correct?

I will test this online and see if it needs to be written in JS as well, thank you for the suggestion! If it needs to be coded in both, do I use the ‘Both’ code type and write one below the other in the same code space?

---

<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:** [May 7, 2020, 3:56pm UTC](https://discourse.psychopy.org/t/multiple-conditions-needed-for-running-a-loop/13095/5 "2020-05-07T15:56:59Z")

</div>

Great, glad I could help! Yes, it needs to go in `Each Frame` - as it will then be continuously checking for a response rather than checking once at the end of the routine. And yep, `Both` is the option you want - that way you can have different JS and Python code in each side, one won’t override the other.

---

<div class="post-metadata">

**Author:** ![Eslifkin](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@Eslifkin](https://discourse.psychopy.org/u/Eslifkin)\
**Post date:** [May 7, 2020, 4:07pm UTC](https://discourse.psychopy.org/t/multiple-conditions-needed-for-running-a-loop/13095/6 "2020-05-07T16:07:29Z")

</div>

Perfect, thank you so much!!
