Selecting pictures into the double digits?

Hi everyone,

I’m currently building a Trail-Making Task in PsychoPy but I’m having some issues. Participants have to click on circles with numbers 1-25 in them, in numerical order. Right now I’m working with some code (see below) from a previous post about the Trail-Making Task, which turns each circle green if clicked in the correct order, and ends the task once each circle has been clicked. My problem is that this only works until the tenth circle. Once it gets to double digits the code won’t allow any clicks on circles, so you can only select circles 1 to 9.

Begin routine:

next_number = 1
clicked_stim =[]

for stimulus in [circle_1, circle_2, circle_3, circle_4, circle_5, circle_6, circle_7, circle_8, circle_9, circle_10, circle_11, circle_12, circle_13, circle_14, circle_15, circle_16, circle_17, circle_18, circle_19, circle_20, circle_21, circle_22, circle_23, circle_24, circle_25]:
    stimulus.color = 'white'

Each frame:

for stimulus in [circle_1, circle_2, circle_3, circle_4, circle_5, circle_6, circle_7, circle_8, circle_9, circle_10, circle_11, circle_12, circle_13, circle_14, circle_15, circle_16, circle_17, circle_18, circle_19, circle_20, circle_21, circle_22, circle_23, circle_24, circle_25]:
    
    if mouse.isPressedIn(stimulus) and not stimulus.color == 'green':
        # check the last character of the stimulus name:
        print(stimulus.name[-1])
        print(next_number)
        if int(stimulus.name[-1]) == next_number:
            stimulus.color = 'green'

            next_number = next_number + 1
            if next_number == 26:
                continueRoutine = False

            break

if mouse.isPressedIn(stimulus):
    clicked_stim.append(stimulus.name)

It was suggested on the previous post to replace the " [-1] " from " if int(stimulus.name[-1]) == next_number: " with " [-2:] ", however that crashes the experiment when a circle is clicked, giving the error " ValueError: invalid literal for int() with base 10: ‘_1’ ".

Does anyone know how I can fix this code to allow clicks up into the double digits? If there is anything here I need to clear up then just let me know!

Thanks in advance!

Hi!

How about using

int(stimulus.name.split("_")[1]) == next_number

instead of

int(stimulus.name[-1]) == next_number

?

2 Likes

That works perfectly! Thanks very much :slight_smile: