# Conditional loops with the mouse component

**URL:** <https://discourse.psychopy.org/t/conditional-loops-with-the-mouse-component/18508>\
**Category:** Builder\
**Created:** [November 30, 2020, 10:50am UTC](https://discourse.psychopy.org/t/conditional-loops-with-the-mouse-component/18508 "2020-11-30T10:50:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lgunning](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lgunning/32/33887_2.png) [@lgunning](https://discourse.psychopy.org/u/lgunning)\
**Post date:** [November 30, 2020, 10:50am UTC](https://discourse.psychopy.org/t/conditional-loops-with-the-mouse-component/18508/1 "2020-11-30T10:50:54Z")

</div>

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

OS: Win10):  
PsychoPy version: 2020.2.2

What are you trying to achieve?:  
I am attempting to create a conditional routine within a loop which is initiated by the mouse component. The task is Sentence Repetition where the participants can rehear the sound by pressing the back button, or go on to the next trial after they’ve repeated the sentence.

What did you try to make it work?:  
I followed this tutorial on branching: [https://www.youtube.com/watch?v=kwbHGnJPEHM&t=337s](https://www.youtube.com/watch?v=kwbHGnJPEHM&t=337s) which was really helpful but doesn’t seem to work with the mouse component (unless I’m doing it completely wrong!). I’ve followed all of the instructions here, but changed this code in ‘End Routine’ based on key responses:  
if(key.oldnew.keys == ‘c’):  
dork = 1  
else:  
dork = 0

(where if you press ‘c’, it employs the loop with nReps$ = ‘dork’ and if you press any other key, it proceeds to the next trial)  
to:  
if(click\_trial == repeat\_icon\_trial):  
repeatsound = 1  
else:  
repeatsound = 0  
(where if you click the polygon called ‘repeat)icon\_trial’ with the mouse component named ‘click\_trial’, the loop with nReps$ = ‘repeatsound’ is employed)

What specifically went wrong when you tried that?:  
The loop/code does allow me to click either the back button or the next button BUT, regardless of which one is pressed, goes onto the next trial (i.e. the loop simply isn’t working and not even throwing up a error message).

I’m a newbie to using code so any help is greatly appreciated!

---

<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:** [November 30, 2020, 11:23am UTC](https://discourse.psychopy.org/t/conditional-loops-with-the-mouse-component/18508/2 "2020-11-30T11:23:41Z")

</div>

Hi There,

I think this demo should do what you need using mouse clicks.[conditionalMouse.psyexp](https://discourse.psychopy.org/uploads/short-url/eehw0GUNrdNDf0cdwQV0B68xPuJ.psyexp) (15.8 KB) [conditions.xlsx](https://discourse.psychopy.org/uploads/short-url/xInSmKSHXIbzzPG7fuuTGlJQxAE.xlsx) (8.5 KB)

Essentially a message is presented (for you this would be a spoken message) and the participants clicks to either hear it again or move onto the next message. It is a little different to your original demo, but should work pretty similarly.

Becca

---

<div class="post-metadata">

**Author:** ![lgunning](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lgunning/32/33887_2.png) [@lgunning](https://discourse.psychopy.org/u/lgunning)\
**Post date:** [December 3, 2020, 5:22pm UTC](https://discourse.psychopy.org/t/conditional-loops-with-the-mouse-component/18508/3 "2020-12-03T17:22:27Z")

</div>

Hi Becca,

Thank you so much for your reply, this is exactly what I want to do!

Unfortunately, although the conditionalMouse task runs wonderfully and I can play around with the code (changing names etc) and it still works, the code doesn’t seem to work within my experiment and throws out the error message:

AttributeError: ‘Mouse’ object has no attribute ‘clicked\_name’

I’m wondering if this is because my clickable options are images, not text…? Or is this completely unrelated and I’m missing something huge…?

---

<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:** [December 3, 2020, 9:34pm UTC](https://discourse.psychopy.org/t/conditional-loops-with-the-mouse-component/18508/4 "2020-12-03T21:34:18Z")

</div>

Hi There,

It should still work fine with image files. In order to have the attribute ‘clicked\_name’ though it is important to make sure the mouse ends on ‘valid clicks’ and that the image component names are listed as ‘clickable stimuli’ - please can you confirm the mouse parameters are identical?

Thanks,  
Becca

---

<div class="post-metadata">

**Author:** ![lgunning](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lgunning/32/33887_2.png) [@lgunning](https://discourse.psychopy.org/u/lgunning)\
**Post date:** [December 4, 2020, 9:09am UTC](https://discourse.psychopy.org/t/conditional-loops-with-the-mouse-component/18508/5 "2020-12-04T09:09:30Z")

</div>

I think so…?

![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/4/a4f04c525be7850402e79022a6b14215542a4992.png)

![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/4/3/43a439389a6f707396d8a9fa2762a3fc87193ef1.png)

It’s still throwing up the same error though.

---

<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:** [December 4, 2020, 9:32am UTC](https://discourse.psychopy.org/t/conditional-loops-with-the-mouse-component/18508/6 "2020-12-04T09:32:11Z")

</div>

I see that you have ‘save mouse state’ as ‘final’ - please can you try changing that to ‘on click’ ? 🙂

---

<div class="post-metadata">

**Author:** ![lgunning](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lgunning/32/33887_2.png) [@lgunning](https://discourse.psychopy.org/u/lgunning)\
**Post date:** [December 4, 2020, 10:15am UTC](https://discourse.psychopy.org/t/conditional-loops-with-the-mouse-component/18508/7 "2020-12-04T10:15:24Z")

</div>

Changed, but still the same error message…?  
I really do appreciate your help with this - I feel like I’m so much closer!

---

<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:** [December 4, 2020, 11:23am UTC](https://discourse.psychopy.org/t/conditional-loops-with-the-mouse-component/18508/8 "2020-12-04T11:23:40Z")

</div>

the code is in the begin experiment tab but I think you want it in the end routine tab 🙂 (clicked\_name isnt defined until something has been clicked)
