# Issues with ending the routine after a specified number of mouse clicks when using "mouse.isPressedIn"

**URL:** https://discourse.psychopy.org/t/issues-with-ending-the-routine-after-a-specified-number-of-mouse-clicks-when-using-mouse-ispressedin/15453
**Category:** Builder
**Tags:** mouse
**Created:** [August 4, 2020, 1:58am UTC](https://discourse.psychopy.org/t/issues-with-ending-the-routine-after-a-specified-number-of-mouse-clicks-when-using-mouse-ispressedin/15453 "2020-08-04T01:58:15Z")
**Posts on this page:** 4
**Page:** 2

<div class="post-metadata">

### Author: ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)
#### Post date: [August 6, 2020, 10:52am UTC](https://discourse.psychopy.org/t/issues-with-ending-the-routine-after-a-specified-number-of-mouse-clicks-when-using-mouse-ispressedin/15453/21 "2020-08-06T10:52:38Z")

</div>

> [@Michael](#):
>
> @jon: Is this behaviour (of colour triplets being returned as `numpy` arrays rather than Python lists or tuples) new, or something intentional that I just haven’t noticed before?

Actually, it was always converted to a numpy array. What’s new I think is that numpy now complains if you do the comparison `np.array([1,2,3]) == [1,2,3]` on the basis that the answer is ambiguous. In some worlds you might consider the answer to be True if _any_ of the elements match. Personally I think that would be weird but strictly it’s true and that’s what numpy decided. So yes we now have to do something like convert to list first or use numpy.any()

---

<div class="post-metadata">

### Author: ![kristylouise](https://avatars.discourse-cdn.com/v4/letter/k/ce7236/32.png) [@kristylouise](https://discourse.psychopy.org/u/kristylouise)
#### Post date: [August 7, 2020, 12:50am UTC](https://discourse.psychopy.org/t/issues-with-ending-the-routine-after-a-specified-number-of-mouse-clicks-when-using-mouse-ispressedin/15453/22 "2020-08-07T00:50:06Z")

</div>

I did still have that statement there, but for some reason it was still reading as a string. I’ve managed to sort that out in the meantime, so the clicks will correctly register as ‘in list’ and ‘not in list’. However, they still won’t change colour (which also means the click count won’t work).

So far, I’ve only tried forcing the array back into a list, as you suggested above. I’m either not doing it correctly, or it just isn’t working for me. I’ve done some reading about numpy.any() and I’m unsure about how to implement it for this scenario, but perhaps it’s worth a shot? Would you mind briefly explaining how to approach that? Sorry again for all the issues - it ended up being a much bigger problem than I imagined.

```auto
    for stimulus in allcircles:
        if unaidedresponses.isPressedIn(stimulus):
            if list(stimulus.fillColor) == [1.,1.,1.]:
                ClickCount = ClickCount + 1
                if stimulus in target_list:
                    stimulus.fillColor == (0,128,0)
                    print ("in list")
                else:
                    stimulus.fillColor == (255,0,0)
                    print("not in list")

```

Thanks for the explanation as well @jon, much appreciated.

---

<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: [August 7, 2020, 2:32am UTC](https://discourse.psychopy.org/t/issues-with-ending-the-routine-after-a-specified-number-of-mouse-clicks-when-using-mouse-ispressedin/15453/23 "2020-08-07T02:32:28Z")

</div>

> [@kristylouise](#):
>
> the clicks will correctly register as ‘in list’ and ‘not in list’… which also means the click count won’t work.

Those two things are mutually contradictory: if your code gets to the point where it is printing out “in list” or “not in list”, then it must have also incremented the click count? You could check by e.g. `print(str(ClickCount1) + ' in list')`

But if those messages are indeed being printed out, then the fill colour must be being set in code. The question then is whether there is a conflict with the Builder settings. Check your circle components. Are their colour fields set to update “every repeat” (they should be, to get re-set at the start of every trial). If they are set to “each frame”, then that could conflict with your code (i.e. it might override your code changes and reset the colour on every frame).

---

<div class="post-metadata">

### Author: ![kristylouise](https://avatars.discourse-cdn.com/v4/letter/k/ce7236/32.png) [@kristylouise](https://discourse.psychopy.org/u/kristylouise)
#### Post date: [August 10, 2020, 12:41am UTC](https://discourse.psychopy.org/t/issues-with-ending-the-routine-after-a-specified-number-of-mouse-clicks-when-using-mouse-ispressedin/15453/24 "2020-08-10T00:41:34Z")

</div>

Sorry - I meant the click count still does not work as I intended (not that it doesn’t work at all).

For anyone looking for a solution to a similar problem, I later worked out that the issue was coming from importing the target list via a .csv/excel file, rather than from the code itself. Even after using the eval(targets) function as recommended above (and a bunch of other methods that I found on Stack Overflow/through other searches), it was still reading the targets as a string rather than a list.

To get around this, I ended up creating a list of targets for each trial in the ‘Begin Routine’ tab. I had six trials in this section, so created six target lists.

For example:  
targetlist1 = [circle16, circle48, circle1, circle23, circle56]  
targetlist2 == [circle47]  
and so on.

For some reason, that made all the difference. I then used the same code that I was working on above::

```auto
        
        if unaidedresponses.isPressedIn(stimulus):
            if list(stimulus.fillColor) == [1.,1.,1.]:
                ClickCount = ClickCount + 1
                #print(type(stimulus))
                if stimulus in targetlist1:
                    stimulus.fillColor = (0,128,0)
                else:
                    stimulus.fillColor = (255,0,0)

    if ClickCount == len(targetlist1) and not trial_ending:
        trial_end_time = t + 0.5
        trial_ending = True 
    if trial_ending and t >= trial_end_time:
        continueRoutine = False 

```

I then just looped through it for all the trials, and it worked perfectly. Thanks Michael for all your guidance and for answering all my questions - I definitely wouldn’t have landed on a solution without your help!

[Previous page](https://discourse.psychopy.org/t/issues-with-ending-the-routine-after-a-specified-number-of-mouse-clicks-when-using-mouse-ispressedin/15453.md?page=1)
