| Reference | Downloads | Github

NameError: name ' ' is not defined

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

OS Win 7:
PsychoPy version 1.90.1:
**Standard Standalone? ** yes
What are you trying to achieve?:
I try to save data depending that depends on what image is presented and what participant chose.

What did you try to make it work?:
I have a code component (code_77) at the beginning of the routine that defines the outcome as “risky” or “safe” depending on what image is shown to participants and what they choose.

The code is as follows:

What specifically went wrong when you tried that?:
I try to safe the response using code component (code_78) in the end of the routine:

#save data
d_lot2.addData('Descr_Choice', choice2)
d_lot2.addData('RT', key_resp_21.rt)
d_lot2.addData('DoorLeft', doorList[9])
d_lot2.addData('DoorRight', doorList[10])

However I get the follwoing error message:

Traceback (most recent call last):
  File "C:\Users\u1664328\Dropbox\1. Projects\Projects\1. Learning from Social & Personal Experience\Experiment 5a and 5b Descriptions - basic and motives\", line 8840, in <module>
    d_lot2.addData('Descr_Choice', choice2)
NameError: name 'choice2' is not defined

I have experienced some timing issues before and this time I tried to move the code that defines “choice2” in the routine before the one that saves the outcomes and also moved code_77 “up” at the beginning of the routine’s view, but it didn’t work.

I stuck with this error for a while now, don’t know how to get around this. Maybe it’s something trivial that I’ve overlooked. What can cause the error in this case?

It seems most likely that choice2 doesn’t get defined because your list of if/elif clauses all fail to be true. Try adding:

    print('choice2 undefined!!')

to the end of that code.

But it does worry me a bit that this experiment is at least 8840 lines long. That seems very large. And I’m hoping all the component numbers (e.g. code_78, image_99) aren’t actual sequential numbers. If they are, that would indicate that the experiment is likely very inefficiently specified, with a worrying amount of repetition.

NB reasons why your if clauses are failing: image components (e.g. image_99) are not equal to the names of the files they display (e.g. 'lot2gamble.png'). The image component is a whole collection of things, and the filename is just one attribute of it. A proper comparison would be to image_99.image. When in doubt as to what the value of a variable is, (temporarily) use debugging code like this:


to make sure that you are comparing the same things. Similarly, the .keys attribute (as the plural implies) is usually a list rather than a single value, so that comparison is probably also failing for you.

Thank you for your reply, Michael!

The experiment is lengthy and inefficient, indeed, but this is the only way I could get the randomisation at all levels of all variables to work using builder. I have 3 levels of loops using conditional branching. I use code component a lot, but I guess I needed to use Excel more instead.

The issue with the ‘choice2’ was resolved by using ‘image_99.image’ instead of ‘image_99’ as you suggested, AND putting the code component (code_77) that defines the outcome as “risky” or “safe” after the routine in which participants make a choice.