Outputting chosen random letters and letter string to CSV

PsychoPy v2021.1.4
Mac OS Catalina v10.15.1

Two questions I have on getting something to output in csv not just log:

In my study I have two conditions - in the first one participants see text cue “VIEW”, see an image, and then respond to a random letter that is either capitalized or lowercase with a 1 = UPPERCASE, 2 = lowercase keypress. To select this random letter each trial, I use the following code:
Begin Routine:
randLetter = random.choice(string.ascii_letters)

Q1: I can see in the resultant log file which letter was and determine the correct answer from that, but struggling to get any of this to be spit out into the csv so I can more easily see on a trial-by-trial basis what participants saw since it will differ for each person. Any suggestions?

Similar for my second condition: they see a series of 6-letters as the cue (instead of ‘VIEW’) and then are to hold the 6-letters in memory until the image disappears. Code:
Begin Experiment:
import random
import string
from string import ascii_uppercase

def choose_letters():
letters = string.ascii_uppercase
return ‘’.join(random.sample(letters, 6))

Begin Routine:
current_list = choose_letters()

Then once the image disappears, they are shown a single letter again (all uppercase this time) and keypress to answer 1 = Yes if the letter is part of 6-letter-string or 2 = No if not. Code:
Begin Routine
randLetter2 = random.choice(string.ascii_uppercase)

Q2: Here I would like to know 1) what the 6-letter string was on that trial (cue) and then 2) what letter was shown that they responded 1 or 2 to. Again so I can infer what correct answer was. I see this data in the log but again I wonder how might I add to code in the loops to have this add to csv?

Q1: You can always choose what data to output in the csv file by adding the following command:

thisExp.addData('random_letter', randLetter)
thisExp.nextEntry() 

See docs.

Q2: Here you must save your answer as well, using either code as explained above or saving the output from a component.

N.B. Only use the next entry code if you need to go to a new line of the data file. If you are using Builder loops then you may already have all the next entries you need.

1 Like

Great - thank you!

Follow-up question: if I wanted to check if the randLetter2 was within current_list on a trial and output a “correct answer” value in the csv how might I do this or at which point within builder would that go? (e.g., begin routine)?

Something like

if randLetter2 in current_list:
     thisExp.addData('Score',1)
else:
     thisExp.addData('Score',0)

in End Routine (i.e. after the current_list has been created).