Conditional loops with the mouse component

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: 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
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)
if(click_trial == repeat_icon_trial):
repeatsound = 1
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!

Hi There,

I think this demo should do what you need using mouse clicks.conditionalMouse.psyexp (15.8 KB) conditions.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.


1 Like

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…?

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?


I think so…?



It’s still throwing up the same error though.

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

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

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