Correct answer variable not found

OS : MacOS Monterey
PsychoPy version (e.g. 1.84.x): v2022.2.4
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?: I’m trying to store the correct answer of a question as a variable called corr_ans. The question is “What was the type of the last question asked? Press 1 for attractiveness, 2 for accuracy, 3 for easy, 4 for flawless.”

What did you try to make it work?:
In the data section of the response properties, I stored the correct answer as $corr_ans, as well as defined corr_ans in the begin routine section in my initial code (see below)


#create list of 4 questions
qlist = ["How ATTRACTIVE is this face?", "How ACCURATE (i.e., knowing the details of the face well, your impression being the same if you saw the face in real life) do you think your impression was?", "How EASY (quick, effortless) was it to make an impression?", "How FLAWLESS is this face in terms of being free of any imperfections?"]

#shuffle the question list
random.shuffle(qlist)

#specify each question according to the randomized order of questions
q1.text = qlist[0]
q2.text = qlist[1]
q3.text = qlist[2]
q4.text = qlist[3]

#if a particular string is in the question, then store the correct answer as 1,2,3, or 4 
if "attractive" in qlist[3]:
    corr_ans = '1'
elif "accurate" in qlist[3]:
    corr_ans = '2'
elif "easy" in qlist[3]:
    corr_ans = '3'
elif "flawless" in qlist[3]:
    corr_ans = '4'

What specifically went wrong when you tried that?:
I get the error message:
Traceback (most recent call last):
File “/Users/jy/Desktop/uncertainty x attractiveness/psychopy /uncertainty_attractiveness_lastrun.py”, line 1203, in
if str(corr_ans).lower() == ‘none’:
NameError: name ‘corr_ans’ is not defined

I’m not sure if I’m doing something wrong with storing my correct answer, or if it has to do with something about where I am defining the corr_ans variable. This question isn’t asked every trial, but rather every 24 trials as an attention check (out of a total 96 trials) so subjects only see it a few times during the experiment. The question successfully only appears 3 times (so I don’t get an error there), but I get an error only when I try to store corr_ans. Any help is greatly appreciated, thank you!

That field is to tell the keyboard component what the correct answer is, not to store it. Otherwise, it can’t know what to compare the actual answer to. In this case, the correct answer is in the variable corr_ans. This would typically would be the name of a column in a conditions file spreadsheet (in which case it would be stored in the data file automatically), but in your case, you are defining it in code.

Because you are creating the correct answer variable yourself, rather than getting it from a conditions file, PsychoPy doesn’t know to save it automatically in the data file. So you need to do so manually on each routine:

thisExp.addData(‘correct_answer’, corr_ans)

The keyboard component will automatically store the actual answer, and whether that answer was correct.

Now the error you are getting is simply because the corr_ans variable hasn’t been defined at the point the keyboard component tries to refer to it.

I can’t see enough of your code to figure that out, but you mention that the variable isn’t needed on most trials, so I would guess it is undefined on other trials. You need to ensure that it exists on every trial, even if it contains just some dummy value.

Thank you for the help, Michael. I tried doing what you suggested and I still get the same error. I tried putting a code component on every routine (more specifically, in the “begin routine” area):

To give you more explanation about the specific routines you’re seeing in the screenshot, I:

  1. Define some initial code like randomization of stimuli in the initCode. This is where I created the list of 4 questions and specified each question according to the randomized order (code in my original post)
  2. Subjects see a fixation
  3. Subjects see a face stimulus (face_stim)
  4. Subjects then see each of the 4 questions in a random order (question_1 ~ question_4)
  5. If the trial number is 23, 47, or 71, continue the routine to show the attention check question (attention_check). If not, skip. I defined this in a code component and it works fine.
  6. Have a text saying “response recorded” (Response_recorded)

This is repeated for 96 trials. Now, the corr_ans variable is only relevant for the attention check question, which asks the type of question subjects saw in the previous question (question 3). I went ahead and deleted $corr_ans from the data properties and added the code:

thisExp.addData(‘correct_ans’, corr_ans)

to every routine, but I still get the same error. Do you know what I’m doing wrong? Also attaching my psychopy file just in case.

Put corr_ans = 0 in Begin Experiment in an Auto translate code component in order to get it defined.

https://psychopy.org/online/psychoJSCodingDebugging.html

Thank you! That worked.