NameError: name 'msgfeedbcon1' is not defined

Dear all,

OS: Mac OS 10.15.7
PsychoPy version: v2021.2.3
Standard Standalone? yes

What are you trying to achieve?:

In the Condition 1 of my experiment I want the participant to be presented with a playing card, followed by a brief pause and then with a feedback message. The participant begins with 2250 euro and he loses or wins money throughout the Condition 1. The money that he loses or wins- and the subsequent feedback that he receives- are predefined in a conditions file. I want, based on specific rows in my conditions file, a message to be presented to the participant with the amount of money that he has at every given time. For example, if he begins with 2250 euro and he wins 50 euro in the first presentation of a playing card, I want him to receive the message " You won 50 euro. Your total amount now is 2300 euro" and so forth.

This is my builder (I highlighted the relevant points):

Very briefly in the content of my loop I have chosen “loopType: sequential” and “Selected rows: 7:23”

And the relevant rows in my conditions file, which is connected with the loop, are:

Inside the Code Component, as you can see in the red square, I have written the following code:

Begin Experiment:

beginamountcon1 = 2250

Begin Routine

if money_1 == 'add50_euro1' :
    total_amountcon1 = beginamountcon1
    total_amountcon1 += 50
    msgfeedbcon1 = "The amount of money that you have now is {} euro.".format(total_amountcon1)

When I run the experiment it begins regularly and shows me all the Components until the “end_of_try_efforts_message2” Component, in Condition 1 it stops and the error that I get is:

52.8307     INFO     Loaded monitor calibration from ['2021_11_28 10:53']
Generating PsychoPy script...

## Running: /Users/pauline/Desktop/Academic/University Of Cyprus/5 Έτος/Εαρινό Εξάμηνο 2021-2022/Εργαστήριο Αναπτυξιακής Ψυχοπαθολογίας/Πτυχιακή Εργασία/Task in Computer/exp1_subgr1_fille/exp1_subgr1_lastrun.py ##
227.8102     INFO     Loaded monitor calibration from ['2021_11_28 10:53']
1.5671     WARNING     We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use ['sounddevice', 'PTB', 'pyo', 'pygame'] (in that order).
2022-02-28 13:25:24.233 python[77437:5182944] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to /var/folders/fv/td94jv0d44jdnknw9qmk77_00000gn/T/org.opensciencetools.psychopy.savedState
Traceback (most recent call last):
  File "/Users/pauline/Desktop/Academic/University Of Cyprus/5 \u0388\u03c4\u03bf\u03c2/\u0395\u03b1\u03c1\u03b9\u03bd\u03cc \u0395\u03be\u03ac\u03bc\u03b7\u03bd\u03bf 2021-2022/\u0395\u03c1\u03b3\u03b1\u03c3\u03c4\u03ae\u03c1\u03b9\u03bf \u0391\u03bd\u03b1\u03c0\u03c4\u03c5\u03be\u03b9\u03b1\u03ba\u03ae\u03c2 \u03a8\u03c5\u03c7\u03bf\u03c0\u03b1\u03b8\u03bf\u03bb\u03bf\u03b3\u03af\u03b1\u03c2/\u03a0\u03c4\u03c5\u03c7\u03b9\u03b1\u03ba\u03ae \u0395\u03c1\u03b3\u03b1\u03c3\u03af\u03b1/Task in Computer/exp1_subgr1_fille/exp1_subgr1_lastrun.py", line 891, in <module>
    condition1_writtenmessage_feedback_sub1.setText(msgfeedbcon1)
NameError: name 'msgfeedbcon1' is not defined
##### Experiment ended. #####

I have also tried:

if money_1 == 'add50_euro1' :
    total_amountcon1 = beginamountcon1 + 50
    msgfeedbcon1 = "You win 50 euro.The amount of money that you have now is {} euro.".format(total_amountcon1) 

And I have also tried:

if 'add50_euro1' in money_1 : 
    msgfeedbcon1 = "You won!"

But I tend to get the same Error Message:

Generating PsychoPy script...

## Running: /Users/pauline/Desktop/Academic/University Of Cyprus/5 Έτος/Εαρινό Εξάμηνο 2021-2022/Εργαστήριο Αναπτυξιακής Ψυχοπαθολογίας/Πτυχιακή Εργασία/Task in Computer/exp1_subgr1_fille/exp1_subgr1_lastrun.py ##
1489.4338     INFO     Loaded monitor calibration from ['2021_11_28 10:53']
1.3572     WARNING     We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use ['sounddevice', 'PTB', 'pyo', 'pygame'] (in that order).
2022-03-02 09:43:01.457 python[91940:5813431] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to /var/folders/fv/td94jv0d44jdnknw9qmk77_00000gn/T/org.opensciencetools.psychopy.savedState
Traceback (most recent call last):
  File "/Users/pauline/Desktop/Academic/University Of Cyprus/5 \u0388\u03c4\u03bf\u03c2/\u0395\u03b1\u03c1\u03b9\u03bd\u03cc \u0395\u03be\u03ac\u03bc\u03b7\u03bd\u03bf 2021-2022/\u0395\u03c1\u03b3\u03b1\u03c3\u03c4\u03ae\u03c1\u03b9\u03bf \u0391\u03bd\u03b1\u03c0\u03c4\u03c5\u03be\u03b9\u03b1\u03ba\u03ae\u03c2 \u03a8\u03c5\u03c7\u03bf\u03c0\u03b1\u03b8\u03bf\u03bb\u03bf\u03b3\u03af\u03b1\u03c2/\u03a0\u03c4\u03c5\u03c7\u03b9\u03b1\u03ba\u03ae \u0395\u03c1\u03b3\u03b1\u03c3\u03af\u03b1/Task in Computer/exp1_subgr1_fille/exp1_subgr1_lastrun.py", line 886, in <module>
    condition1_writtenmessage_feedback_sub1.setText(msgfeedbcon1)
NameError: name 'msgfeedbcon1' is not defined
##### Experiment ended. ##### 

And of course I have added:

$msgfeedbcon1 #set every repeat 

in the Text Component after the above Code Component (as you can see in the red square)

What did you try to make it work?:
I have read the relevant recommendations in the book “Building Experiments in PsychoPy” by Peirce and MacAskill, and those in this forum. This seems to match with my case: (NameError: name ___ is not defined - #2 by jon
But:

  1. I am writing in a Code Component (and I have chose “Set Every Repeat” in my Loop)
  2. I don’t think it is a typographical error, because I don’t have written this variable anywhere else (rather than the followed Text Component)
  3. I tried to initialise my variable in the “Begin Experiment” , like msgfeedbcon1 = “” but I get a space rather than the desired message
  4. I have tried to delete the files " ~/.psychopy2/appData.cfg" and “~/.psychopy2/userPrefs.cfg” in the PsychoPy Coder>Shelf>Shell, but the error that I get is:
PyShell in PsychoPy - type some commands!

Python 3.6.8 (v3.6.8:3c6b436a57, Dec 24 2018, 02:04:31) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
rm~/.psychopy3/appData.cfg
  File "<input>", line 1
    rm~/.psychopy3/appData.cfg
      ^
SyntaxError: invalid syntax

I get the same message with “psychopy2” in the place of “psychopy3”.

  1. I have reset the preferences of my experiment, as PsychoPy Preferences → App tab → reset preferences and restart and open your code (I am not sure how to restart and open my code) as mentioned in (NameError: name ___ is not defined - #6 by Liner)

The same NameError message was appearing every time, and not only the message was appearing appropriately a couple of days ago, but I have an other code component with the following code that works perfectly:

Begin Experiment

myCount = 0

Begin Routine

myCount = myCount + 1
if myCount > 4:
   try_efforts1_loop.finished = True
if feedbackadjusted == 'Positive Reinforcement' :
    msg = "You win money"
    msgcolor = "green"
elif feedbackadjusted == 'Negative Reinforcement' :
    msg = "You lost money"
    msgcolor = "red"

Thank you very much for your time,
Kind Regards,
Pauline

Hello,

ok, you need to initialize msgfeedbcon1 before you use. The fact that you only get a space when initializing it with

msgfeedbcon1 = " "

rather than your intended message hints towards a wrong if-construction. Does your if-construction ever evaluates as true? You can test it by adding a

if money_1 == 'add50_euro1' :
    print("if-entered")
    total_amountcon1 = beginamountcon1 + 50
    msgfeedbcon1 = "You win 50 euro.The amount of money that you have now is {} euro.".format(total_amountcon1) 

or adding an else

if money_1 == 'add50_euro1' :
    total_amountcon1 = beginamountcon1 + 50
    msgfeedbcon1 = "You win 50 euro.The amount of money that you have now is {} euro.".format(total_amountcon1) 
else:
   msgfeedbcon1 = "not true"

Best wishes Jens

Is condition1_writtenmessage_feedbak_sub1 set to Constant or Each Repeat? It might be trying to use msgfeedbcon1 too early.

Jens’ solution is for an additional online issue, but I don’t think it’s your main problem here.

Hello Mr Jens,

Thank you very much for taking the time to reply to my question.

Regarding your suggestion:

  1. By having in Condition 1

Begin Experiment

beginamountcon1 = 2250

and

Begin Routine

if money_1 == 'add50_euro1' :
    print("if-entered")
    total_amountcon1 = beginamountcon1 + 50
    msgfeedbcon1 = "You win 50 euro.The amount of money that you have now is {} euro.".format(total_amountcon1)

I get the Error Message:

39.2810     INFO     Loaded monitor calibration from ['2021_11_28 10:53']
Generating PsychoPy script...

## Running: /Users/pauline/Desktop/Academic/University Of Cyprus/5 Έτος/Εαρινό Εξάμηνο 2021-2022/Εργαστήριο Αναπτυξιακής Ψυχοπαθολογίας/Πτυχιακή Εργασία/Task in Computer/exp1_subgr1_fille/exp1_subgr1_lastrun.py ##
122.9561     INFO     Loaded monitor calibration from ['2021_11_28 10:53']
0.8453     WARNING     We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use ['sounddevice', 'PTB', 'pyo', 'pygame'] (in that order).
2022-03-03 09:49:06.884 python[97949:6068352] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to /var/folders/fv/td94jv0d44jdnknw9qmk77_00000gn/T/org.opensciencetools.psychopy.savedState
Traceback (most recent call last):
  File "/Users/pauline/Desktop/Academic/University Of Cyprus/5 \u0388\u03c4\u03bf\u03c2/\u0395\u03b1\u03c1\u03b9\u03bd\u03cc \u0395\u03be\u03ac\u03bc\u03b7\u03bd\u03bf 2021-2022/\u0395\u03c1\u03b3\u03b1\u03c3\u03c4\u03ae\u03c1\u03b9\u03bf \u0391\u03bd\u03b1\u03c0\u03c4\u03c5\u03be\u03b9\u03b1\u03ba\u03ae\u03c2 \u03a8\u03c5\u03c7\u03bf\u03c0\u03b1\u03b8\u03bf\u03bb\u03bf\u03b3\u03af\u03b1\u03c2/\u03a0\u03c4\u03c5\u03c7\u03b9\u03b1\u03ba\u03ae \u0395\u03c1\u03b3\u03b1\u03c3\u03af\u03b1/Task in Computer/exp1_subgr1_fille/exp1_subgr1_lastrun.py", line 900, in <module>
    condition1_writtenmessage_feedback_sub1.setText(msgfeedbcon1)
NameError: name 'msgfeedbcon1' is not defined
##### Experiment ended. #####

Although, by adding an ‘else’ statement (same Begin Routine) I see in the place of my intended message the “not true” message.

So I suppose my if-construction is wrong, could you give me any further suggestions on how this if-construction can be true?

Thank you very much for your time Mr Jens.

Hello,

the error tells you that msgfeedbcon1 is not defined, so add

msgfeedbcon1 = "oops"

in your Begin experiment tab. If msgfeedbcon1 will always be “oops” throughout the experiment, your if-construction never evaluates as true.

Best wishes Jens

1 Like

Hello Mr Morys-Carter,

I would also like to thank you for your time that you take to respond to my problem. I have set the Text Component named “condition1_writtenmessage_feedback_sub1” to be “Every Repeat”.

By setting this to be “Constant” and having

Begin Experiment

beginamountcon1 = 2250

Begin Routine

if money_1 == 'add50_euro1' :
    total_amountcon1 = beginamountcon1 + 50
    msgfeedbcon1 = "You win 50 euro.The amount of money that you have now is {} euro.".format(total_amountcon1)

Unfortunately the experiment doesn’t start and the message I get is:

Generating PsychoPy script...

## Running: /Users/pauline/Desktop/Academic/University Of Cyprus/5 Έτος/Εαρινό Εξάμηνο 2021-2022/Εργαστήριο Αναπτυξιακής Ψυχοπαθολογίας/Πτυχιακή Εργασία/Task in Computer/exp1_subgr1_fille/exp1_subgr1_lastrun.py ##
2009.3845     INFO     Loaded monitor calibration from ['2021_11_28 10:53']
1.4674     WARNING     We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use ['sounddevice', 'PTB', 'pyo', 'pygame'] (in that order).
8.6466     WARNING     t of last frame was 20.12ms (=1/49)
2022-03-03 10:20:34.258 python[98320:6085667] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to /var/folders/fv/td94jv0d44jdnknw9qmk77_00000gn/T/org.opensciencetools.psychopy.savedState
Traceback (most recent call last):
  File "/Users/pauline/Desktop/Academic/University Of Cyprus/5 \u0388\u03c4\u03bf\u03c2/\u0395\u03b1\u03c1\u03b9\u03bd\u03cc \u0395\u03be\u03ac\u03bc\u03b7\u03bd\u03bf 2021-2022/\u0395\u03c1\u03b3\u03b1\u03c3\u03c4\u03ae\u03c1\u03b9\u03bf \u0391\u03bd\u03b1\u03c0\u03c4\u03c5\u03be\u03b9\u03b1\u03ba\u03ae\u03c2 \u03a8\u03c5\u03c7\u03bf\u03c0\u03b1\u03b8\u03bf\u03bb\u03bf\u03b3\u03af\u03b1\u03c2/\u03a0\u03c4\u03c5\u03c7\u03b9\u03b1\u03ba\u03ae \u0395\u03c1\u03b3\u03b1\u03c3\u03af\u03b1/Task in Computer/exp1_subgr1_fille/exp1_subgr1_lastrun.py", line 187, in <module>
    text=msgfeedbcon1,
NameError: name 'msgfeedbcon1' is not defined
##### Experiment ended. #####

But again, thank you very much for your time and response Mr Morys-Carter!

Hello Mr Jens,

Indeed, by having:

Begin Experiment

beginamountcon1 = 2250
msgfeedbcon1 = "oops"

Begin Routine

if money_1 == 'add50_euro1' :
    print("if-entered")
    total_amountcon1 = beginamountcon1 + 50
    msgfeedbcon1 = "You win 50 euro.The amount of money that you have now is {} euro.".format(total_amountcon1)

or

if money_1 == 'add50_euro1' :
    total_amountcon1 = beginamountcon1 + 50
    msgfeedbcon1 = "You win 50 euro.The amount of money that you have now is {} euro.".format(total_amountcon1)

The message that I get throughout the experiment is " oops". If you allow me, do you have any suggestion on how this if-construction can be evaluated as true?
Thank you again for your time and response.

Hello,

does this toy-experiment do what you expect?

money.psyexp (10.6 KB)
MoneyStim.xlsx (17.9 KB)

Best wishes Jens

I think it might be the sequential execution of the components like this:

But the right thing to do is

Hello Mr Jens,

Shortly before you send me your suggestion I had changed the “Selected Rows” of the loop in Condition 1 and the experiment seems to work as I want. [page183, “Presenting Only A Subset of Conditions” in book "Building Experiments in PsychoPy” by Peirce and MacAskill]

More specifically to whom it may concerns:

  1. My condition file is:

  2. I change the “Selected Rows” of my “try_efforts1_loop” to 0:5 and the “Selected Rows” of “condition1_loop” to 5:22.

My builder:

And the content of loops:

  1. Some of my code in “Begin Routine” of the Code Component in Condition 1, as you can see in the red square is:
if money_1 == 'add50_euro1' :
    totalamount_con1 = 2250 + 50
    msgfeedb_con1 = "You win 50 euro. The amount of money that you have now is {} euro.".format(totalamount_con1)
elif money_1 == 'rem50_euro1' :
    totalamount_con1 = totalamount_con1 - 50
    msgfeedb_con1 = "You lost 50 euro. The amount of money that you have now is {} euro.".format(totalamount_con1)
elif money_1 == 'add50_euro2' :
    totalamount_con1 = totalamount_con1 + 50
    msgfeedb_con1 = "You win 50 euro. The amount of money that you have now is {} euro.".format(totalamount_con1) 

and the Text Component after the above Code Component, as you can see in the red square is:

So I suppose the wrong if-construction had to do with a connect in an inappropriate row based on the conditions file.
Once more, thank you very much Mr Jens for your time and suggestions which directed me towards a wrong if-construction and eventually solve my problem.

Yours Sincerely,
Pauline

Dear Wen,

Thank you very much for your response and your careful observation. The problem was focused on the Components in red square as :

which are already placed as you suggest me :slight_smile:

The Text Component above the first Code Component serves a brief lack of any stimulus (I have left the text blank) after the presentation of the playing card (first Component). After that, the first Code Component serves for choosing the right feedback message based on the conditions file and the second Text Component presents that feedback message.

Thank you again for your time and your response!

I’m glad I could help