psychopy.org | Reference | Downloads | Github

Adding text to new data column on pressing return

Hi,

I’m trying to create a word association experiment, wherein participants have to respond to a stimulus word with any words which enter their mind in response to the stimulus presented, until the stimulus disappears after 60 seconds.

With the help of some forum members, I’ve got to a point where the experiment basically works - participants can enter the words they think of, see them on screen, and modify them. The words then they get sent to a data file. However, I’d really like two additional things to happen. The main code for doing the above is in the “each frame” section:

keys = event.getKeys()
if len(keys):
    if 'space' in keys:
        text.text = text.text + ' '
    elif 'backspace' in keys:
        text.text = text.text[:-1]
    elif 'lshift' in keys or 'rshift' in keys:
        modify = True
    elif 'escape' in keys:
        core.quit()
    elif 'return' in keys:
        text.text = text.text + '\n'
    else:
        if modify:
            text.text = text.text + keys[0].upper()
            modify = False
        else:
            text.text = text.text + keys[0]

and the following in “end routine”:

thisExp.addData("typedWord", text.text)

However, this results in a line-separated string of responses being placed into a single column, “typedWord”. This isn’t ideal because it leaves me needing to post-process those responses by moving each response into its own column (i.e. columns named "response_1, response_2, etc.). So my two questions. Firstly, is their any way to have Psychopy create a new data column for each response entered? So that if my participant types “hope [return] glory [return]” (up to a theoretically unlimited number of responses), I get a csv file with a column “Response_1” with “hope”, and “Response_2” with “glory”?

And secondly, I’d like each response to disappear once typed (and sent to its unique column), so that participants are discouraged from responding not to the stimulus but to their own previous response (i.e. chaining). I’ve tried this using:

    elif 'return' in keys:
        text.text = ' '

This has the desired effect on screen, but unfortunately it also results in only the last response to each stimulus being sent to the data file.

Many thanks,

Peter

Win10
PsychoPy 3.1.5
cond.xlsx (8.1 KB) 2019 WA practice file.psyexp (13.0 KB)

The issue here is that you need to be storing responses in variables, rather than in the text attribute of your stimulus. This gives you a lot more flexibility, as you can separate the display and storage of the data. I’d suggest you create two variables in the “Begin routine” tab:

current_word = ''
stored_words = []

and then something like this in the “each frame” tab (have modified the previous code as it was mixing up whether there could be one or more keys returned):

keys = event.getKeys()
for key in keys:
    if key == 'space':
        current_word = current_word + ' '
    elif key == 'backspace':
        current_word = current_word[:-1]
    elif key == 'lshift' or key == 'rshift':
        modify = True
    elif key == 'escape':
        core.quit()
    elif key == 'return':
        stored_words.append(current_word) # store
        current_word = ''              # clear
    else:
        if modify:
            current_word = current_word + key.upper()
            modify = False
        else:
            current_word = current_word + key

# update if necessary:
if text.text != current_word:
    text.text = current_word

Lastly put something like this in the “end routine” tab:

# remember to store any letters typed that weren't terminated 
# with a return key:
if current_word != '':
    stored_words.append(current_word)

# save each word in a numbered column:
for i, word in enumerate(stored_words):
    thisExp.addData('typed_word_' + str(i), word)

Hi Michael,

Many thanks for the reply. I’ve input the new code but I’m getting an error message:

image

The coding level here is beyond my skill level - I’ve tried to figure out what the problem is but with no success. Any ideas?

Peter

I guess modify needs to be defined before this code runs. Not sure why that error wouldn’t have occurred before, but I’d suggest you simply add this in the “begin routine” tab:

modify = False # track whether the shift key has been pressed.

That works! Thanks very much!