Using Button and Mouse responses to add or delete items from a list

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

OS (e.g. Win10): Mac 12.2.1
PsychoPy version (e.g. 1.84.x): 2021.2.3
Standard Standalone? (y/n) If not then what?: Yes
What are you trying to achieve?:


I am trying to recreate the automated OSPAN task (Unsworth et al., 2005) and I am having a difficult time getting the “clear” and “blank” buttons to work the way I want them to work for the recall portion of this task.

Among other tasks, participants are asked to remember a set of letters in a specific order. For example, they may be asked to remember “F P N Q” and then must make button responses (using a mouse) to recall the letters they were asked to remember. Every letter they select is also present at the bottom of the screen. The screen should look something like this (mine does not have the squares next to the letters).

Screen Shot 2022-05-11 at 5.20.13 PM

If a participant only remembers “F P _ Q” and does not remember “N” the participant should have the option to add a space to help preserve the order.

If the participant initially recalled “F P Q N” and realized the “N” and “Q” are reversed, the participant should be able to delete the last two letters and re select the letters in the proper order.

In the task I am building, the letters are clickable text stimuli via a mouse component. I also have three button response components participants can press if they need to add a space (blank), delete a letter (clearbutton), or move on to the next block (exit). An image of the builder view is below, if it is useful.

My initial thought was simply to write a few statements to the effect “If the blank button is clicked: add a space (i.e., “__”)” or “If the clearbutton is clicked: delete the previous letter”. However, I have not found a way to do this with either a code component or the Callback Function in a button component. I am using screen_text="".join(mouse.clicked_name) to collect (and ultimately display) responses and I am assuming this is creating a list. I tried mouse.getPressed and mouse.isPressedIn in a few if statements, but I am not able to produce the desired outcomes. I tried the statements below as a code component under the Every Frame tab but get errors.

First I tried:
if mouse.getPressed(blank): screen_text=" ".join(mouse.clicked_name + "__")
Here I would get the following error: TypeError: can only concatenate list (not "str") to list

I also tried append: if mouse.getPressed(blank): screen_text.append("__")
But here I get this error: AttributeError: 'str' object has no attribute 'append'

I also tried this to delete the last item in a list: if mouse.getPressed(clearbutton): screen_text = mouse.clicked_name.pop()
Here the experiment fails after I enter a subject ID with the following error: IndexError: pop from empty list

I am sure the answer is relatively straight forward, but I have not been successful in coming up with a solution. I suspect I misunderstood either the mouse component, the the button component, and/or how clicked items are actually being stored, but I am not sure. Any thoughts?