Understand code component tabs

OS (Win10)
PsychoPy version (2020.2.4)
Standard Standalone? (y)

I have some working code, but need help figuring out where to place what in the code component tabs. I will present sentences of different set length, then people will recall the last word of each sentence by typing out the words, the key-presses will be stored as a list of letters. The program first extract the final words and store them in a list, and then compare this list with the input list, and count how many words participant recalled correctly. Here’s the example code. I want the feedback to show up after recall is completed, indicated by a press of the space bar.

sentence=['The bird is red', 'Look, that is a red bird', 'We went to see the ocean']
final_words= [i.split()[-1] for i in sentence] # Extract the last word

input_letters = ['b','i','r','d','r','e','d','o','c','e','a','n'] # this would become something like key_resp_2.keys or similar in the actual experiment

input_string = ''.join(input_letters)  # This becomes 'birdredocean'

matched_words  = [ ]
for word in final_words:
    if word in input_string:
        matched_words.append(word)

print("you got %s out of %d correct" %(len(matched_words), len(stimuli_words)))

If the sentences change on each trial, the lines where you extract the last words belong in the “Begin routine” tab, as they need to be available for the stimuli to use on this trial. If the sentences are constant throughout the session, then that code only needs to run once, so it should go in the “Begin experiment” tab to avoid repetition.

The lines where you join the typed letters and check for matches are best in the “End routine” tab, as only then is the complete set of keyboard responses available to you.

Setting the feedback might best be placed in the “Begin routine” tab of the next routine, so that the message is available to be used by a text stimulus there. But there is some choice here - it can also go in the previous “end routine” tab to keep all the code together. All that matters is that you create the message variable before the text stimulus on the next routine tries to use it.

If for some reason you wanted to respond to individual keypresses in real time, rather than waiting to process all of them at the end of the routine, then such code would need to be in the “Each frame”, so that it runs once per screen refresh during the trial.

If you want to know where your code gets inserted, put in a distinctive comment, like # xxx. Then push the “compile script” button on the toolbar, and search for that comment string.

Thanks Michael, this was helpful. It took me a while to figure out the order of things within the tab, but my experiment is working now!