IndexError in evaluating if mouse.clicked_name[0]==

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

OS MacOS Sonoma 14.0
PsychoPy version 2023.2.3
Standard Standalone? (y/n) Y

What are you trying to achieve?:

I’m attempting to run a vowel identification task (participant hears a vowel in a carrier sentence and clicks the word containing a rhyming vowel). There are 5 on-screen response buttons and the mouse is locked to a dock until a certain time passes which then frees the mouse for them to click.

I’ve done a ‘practice’ module here that gives feedback (I’m still in the process of updating how the feedback works, but the error described here is making that tedious). The implementation of the feedback isn’t quite right yet, but here’s the issue gumming up testing it:

I evaluate where each trial is correct at the end of the Trial routine in a code chunk “checkCorr” and then use info from there to update the feedback routine that come next.

What did you try to make it work?:

I’ve inserted this code chunk where intPhon is a variable in my conditions file.

if mouse.clicked_name[0]==intPhon:
    correct = 1
    correct = 0

What specifically went wrong when you tried that?:
It works great most of the time. I have successfully run through one or two blocks of trials a number of times, but sometimes it will stop and give the following error (screenshot below, copy paste isn’t working): ''if mouse.clicked_name[0]==intPhon: IndexError:list index out of range".

vit_err_toshare.psyexp (75.6 KB)

It should only be accepting valid clicks, which are named identically to the values in intPhon. It works marvelously most of the time, so it appears to be doing that.

I can’t find a rhyme or reason to when the error occurs: it happens on correct and incorrect responses, for different items in the list, at different trial numbers, on the first and second loop.

Any insight is much appreciated. I’ll attach the psyexp file here as well.

Include pasted full error message if possible. “That didn’t work” is not enough information.

This will fail if nothing has been clicked. Can the routine time out? You could start with if mouse.clicked_name:


As it’s currently written, no, the routine should not time out. It only ends on a mouse click of a valid response. This error only happens upon clicking. I meant to mention that before.

I just went through and checked for things that might be timing out and took duration out of two components (audio was set to its duration; instructions were set to 3s) that needlessly had one specified. (Updated file here:
vit_err_toshare.psyexp (75.6 KB) )

Same error occurred. I’ve also let it sit for several seconds before responding without causing the error to occur.

I’m not sure I understand your solution. Are you saying use ‘mouse.clicked_name’ without the index [0]?

You could try without [0] if the mouse ends the response. I was suggesting checking that the clixked name exists before checking the first name in the list.

Leaving out the [0] didn’t work: it runs but doesn’t actually have anything to check.

I implemented the following, and it’s run without issue several times right now. So the initial if statement is just checking that the name exists. If it doesn’t exist, then, nothing would be evaluated and it would just move on without completing that process.

# evaluate trial response

if mouse.clicked_name:
    if mouse.clicked_name[0]==intPhon:
        correct = 1
        correct = 0

Would it be a good check to add an else statement to the outside if loop there with something like “Print(No mouse click item)”? Thans for your thoughts.

That is the construction I was initially suggestion. You could add else: correct = -1 at the bottom to identify the trials where there was no mouse.clicked_name. Otherwise, correct will be the same as the previous trial.

Right. It makes sense to me now that I’ve done it and seen it working. I’ll add a line like that so I can add a behavior that shows me an error in the experimenter.

Thanks so much. I’ll mark your earlier comment as the solution.

I write to chime in with additional details to see if there’s more info I can get on what’s causing this issue.

It’s still solved in that I have followed the solution above and that permits me to run the experiment through without crashing in these trials where it seems to not register the mouse click correctly. I worked my code chunks such that it writes and version of an error message to the output spreadsheet in the trials where the behavior is occurring. It has now let me see what else is happening in those trials. Here are my mouse component settings and a code chunk I wrote that allows it to continue without crashing on those trials.

In my attached spreadsheet
test_data_toshare.csv (637.1 KB)
, I’ve highlight in yellow the trials where this problem occurs. The mouse component is set up to only record valid clicks, the 5 response buttons, and end routine on any valid click. No components time out, so there nothing else can end the routine. The issue is that on certain trials PsychoPy appears to “accept” a valid click but not record it. So ‘mouse.clicked_name[0]’ was empty for some reason, and the columns ‘mouse.clicked_name’ and any that I created referencing it (‘answer’, ‘ansCorr’, and ‘respTime’) are empty.

However, and this is why I’m curious enough to respond here again, in each of those trials the colums corresponding to each response box ‘[name].numClicks’ register 1 in what I assume to be the chosen selection.

I would love any ideas on what might be happening here. I can’t find any rhyme or reason as to the trials on which this issue occurs. I thought it might be a hardware issue, but switching from the wireless mouse I use on my laptop day to day to a wired one didn’t stop the behavior.