Name error after using polygon feature

Hi, I am receiving error message
“polygon.setFillColor(color)
NameError: name ‘color’ is not defined”

I am trying to use the branching technique so that the participant in my experiment will respond ‘m’ for the presence of a previously listed target and ‘z’ for the absence of a previously listed target. After the response is given during a mask, a screen should show up indicating either a +10 or 0 in green reward if a correct answer is provided, or a -10 or 0 reward in red if a negative answer is provided. In the advanced polygon tab, we have fill color as $color and it is set as every repeat. The following are images of the experiment and excel file

If there are any errors you can see that would cause this message to occur, please let me know.

Thanks!

This error is usually because you have used the variable name color in a field that is set to “constant” rather than to update “every repeat”. Constant values are set at the beginning of the experiment, before your loop gets created, so the name of the variable isn’t available to PsychoPy yet. You want the field to update, so set it accordingly.

PS this way of conditional branching still works, but it is an old-fashioned work-around. You no longer need to use loops and repetition numbers for this. Instead, you can just put some code like this in the “begin routine” tab of a code component:

if some_negative_condition: # whatever test is relevant to you
    continue_routine = False # don't even start this routine

This is a much more direct process, and doesn’t involve the messy use of loops for something other than they were intended for.

Hi Michael,
Thank you so much for your response and advice. I am very new to PsychoPy and also have never coded before. While your suggested method seems more direct, I’m not sure I have the coding knowledge to perform it successfully. I used a tutorial to code what I have already screenshotted above.

In regard to the color field, I have checked the experiment and the only place that $color is defined is in the trial one tab and it is set as every repeat, not set as constant. Is there any advice you have as to what I can do to get the experiment working with the coding I have already done instead of changing the approach? This is what I have done already regarding color.


The first time that we have a input in PsychoPy is in the first trial. If there is a place where we should define color sooner, please let me know.

Thank you so much again.

If it works for you, then stick with it.

Going back to your original post, there seem to be two conditions files (reward1.xlsx, which contains color, and conditions.xlsx, which doesn’t).

Your trial routine is enclosed only by the loop called trials. Therefore that loop needs to be connected to a conditions file containing a variable called color. I can’t tell from the screenshots above what each file is for or which loops they are connected to, but you need to ensure that any references to a variable are within the scope of an enclosing loop that is connected to that variable in its conditions file. i.e. a variable isn’t “visible” to anything outside of the loop that has a reference to it.

Hi Michael,
Thank you so much for another response. Sorry for all of the confusion. I wound up taking out the polygon feature in hopes of finding a more direct way of coding/branching. Instead of using the polygon feature, I created two separate conditions files (one if the participant answers correctly, in which a randomly selected value of 0 or 10 points will be shown to the participant in green, and another for if the participant answers incorrectly, in which a randomly selected value of 0 or -10 in red will be displayed). I imported the corresponding reward files in their designated loops: the correct answer excel sheet in the loop around correct trial, and the incorrect answer excel sheet in the loop around the incorrect trial. There is still a blockSelect routine that has coding.

In addition to this, I have an original conditions file imported to the big trial loop, which contains cues, images, and 2s and 3s under a rewardAmt title, which should correspond to lines two and three in the correct and incorrect excel sheets, indicating the amount for reward. After running the experiment, this is the new error message.

If you have any new advice, please advice. So sorry again for all of the confusion. Thank you so much for all of your help, it is greatly appreciated.

I might be alone on this one, but thought I’d add a related problem that I came across. The issue was that the code kept crashing because it said “black was not a color”, even though I knew that if I called the color from a condition file it would work just fine.

It’s quite obvious if you think about this in code rather than GUI - the issue is that it’s interpreting black as a variable name. I just needed to change it to “black” to explicitly tell Python that this is a string. Then when it set the color it was reading the string. Of course, when it’s reading from the condition excel file, the word “black” is being read in as a string, which is why that worked but mine didn’t.

I discovered the problem by clicking the “< module >” bit of the error, which brought up the Coder, and then I realised how it was trying to use the info I was giving it.