Weird NaN print issue

13 views
Skip to first unread message

dbor...@willamette.edu

unread,
Sep 2, 2025, 1:09:09 PMSep 2
to Glowscript Users
Hey there, Steve/Bruce!

One of my students ran into an interesting issue in Web VPython. 

Here's the minimal working code:

Web VPython 3.2
print(((-b+sqrt((b**2)-4*a*c))/(2*a)))
a = 3
b = 5
c = -2

If you run this in Web VPython, the print statement prints "NaN." If you run this in raw Python, it throws and error since a, b, and c aren't defined at the point that the print statement runs. In Web VPython, if you comment out or delete one of the variable definitions, then a "ReferenceError: [variable] is not defined" error is actually thrown. Not sure if this is a known issue (or if it is fixable or even something you would want to fix) but I figured I would point it out in case it matters.

Best,

Daniel

Steve Spicklemire

unread,
Sep 2, 2025, 3:28:02 PMSep 2
to 'Jim D.' via Glowscript Users, Steve Spicklemire
This is a consequence of the design of WebVPython. It transpiles python into javascript and runs the javascript in your browser. In javascript variables that are not yet assigned literal values, but are declared as “var” are initially assigned “undefined”. This is what’s causing the “NaN”. In python, variables that are not yet assigned are not defined as anything, so you’ll get NameError.
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/glowscript-users/e8f71fbe-99b4-4880-8b3d-69e1418eff2cn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages