NameError: name 'nan' is not defined (the English word "nan" vs. python language "NaN")

OS: macOS Mojave (version 10.14.6)
PsychoPy version: v2021.2.1
**Standard Standalone? : yes

What are you trying to achieve?:
I’m trying to display a list of 6 English words in a random order, including the word “nan”.

What did you try to make it work?:
I inserted a routine which contains a text component (to display the 6 words according to a spreadsheet) and a keyboard component (to move to the next word). I also inserted a loop around this routine. Please see screenshot below for the design.

What specifically went wrong when you tried that?:
The experiment won’t start properly because of the following error message:

File “”, line 1, in
NameError: name ‘nan’ is not defined

However, when I remove “nan” from the 6 words OR when I just include “nan” in the text component, the experiment will start properly. I’m not very certain about this, but maybe this has something to do with the system confusing the English word “nan” with Python code “NaN” (Not a Number)? If so, is there any way I can fix this problem?

Hi @Jenny_rj ,

Yes I can reproduce this. Three things you can do about this (I think the last one is perhaps the best).

  1. Replace the words with images of words OR
  2. Add spaces between the letters OR
  3. In your excel conditions file replace the word nan with another word, say dummyword. Then in the trial routine add a code component and place the following snippet in the Begin Routine tab (mywords here is the header of my excel column, change accordingly). Place the code component on the very top
if mywords == 'dummyword':
    mywords = 'nan'

NOTE: if you do solution 3 then in your results the dummyword will be saved, not the nan. Of course you can save your own column or words at the end that will include the correct words.

I hope this helps.

Best wishes

Hi Yiannis,

I tried solution 3 and it worked! Thank you very much for your advice!

Best regards,