Error: Unable to convert nan to numerical

Hi all!

Context: I am using arrow keys to adjust the height of a bar continuously changing in height. I am trying to stop participants from adjusting the bar’s size to <0 by counting how many down presses they make, and not applying adjustments to the bar after they have made enough keypresses to make the bar’s size <0.

Here is the code I use to try do this:

Unfortunately, after several down key presses I get the Error: Unable to convert nan to numerical. This error disappears if I remove line 12: if DownKeyCount < 8:

Does anyone have any idea why this line of code appears to be triggering a nan to numerical error?

Thank you in advance!

Hello,

you could start by adding some print-commands, e.g. print('DownKey', DownKey) aso. to your script and see what values your variable have?

Best wishes Jens

1 Like

Thanks for the tip Jens.

I ran print to the down keys value, and it is quite weird. Sometimes the error is triggered when the down key count is 3, sometimes it triggers at 7, sometimes it triggers anywhere between, or when the value is greater than 8. The error seems to just keep occurring sporadically.

The error I am consistently getting is Unable to convert NaN to a number

This is pretty bizaare. I have no idea when or why the variable is becoming a NaN. The code I provided initially is the code that controls the variables value.

Hello TIm,

you need to know the values of the other variables as well not just the value of DownKeyCount. DownKeyCount doesn’t seem to be the critical variable because it is never NaN when your code broke, right?

BTW, do you reset the values of all relevant variables at the end (or begin) of the routine?

Best wishes Jens

Hi Jens,

Thank you for your assistance! I suspected it was DownkeyCount, as the problem emerged after I added that variable. However, it was actually another variable, adjust, which was breaking when I rounded it like so: round(Adjust, 5).

After using print() I was able to find out that round was breaking all the variables it was used on. Seems like the rounding with decimal values just was not working well on this experiment for some reason.

I fixed the problem by replacing the old round(adjust, 3) code with: Adjust = round(Adjust * 1000) / 1000

Thank you so much for your assistance! I am glad I got this fixed!

Hello,

cool that the experiment is up and running.

Best wishes Jens