About global variables

23 views
Skip to first unread message

EDUCATIA SUPERLATIV

unread,
Mar 4, 2026, 8:03:35 AMMar 4
to VPython-users
Hi,

I did use to define a global variable outside of a function,
but when I try to modify it inside of a function it give me
error 
" ReferenceError: Cannot access 'C0' before initialization"

I use Vpython in Html!

Some docs said I should use "global" keywords inside function,
but it doesnt work!

How can I solve this issue, please!
Add, here a short example, please...

Best regards,
Ciprian

John

unread,
Mar 4, 2026, 11:20:22 AMMar 4
to VPython-users
I posed your question to Google Gemini AI  and here is what it said.

https://gemini.google.com/share/ce5fa00028f0

EDUCATIA SUPERLATIV

unread,
Mar 5, 2026, 8:15:01 AMMar 5
to vpytho...@googlegroups.com
Many thanks, but, my definition is according to next Gemini response

See here a sequence of my code

”let C0 = [];
async function deploi_animate(index, time){
//global C0;
if (index == 0) {
if (time == 0) {
let colorCUB2 = color.yellow
let v0F1 = vertex({pos:vec(-1,-1,1), color:colorCUB2, opacity:0.5})
let v1F1 = vertex({pos:vec(1,-1,1) ,  color:colorCUB2, opacity:0.5})
let v2F1 = vertex({pos:vec(1,-1,-1), color:colorCUB2, opacity:0.5})
let v3F1 = vertex({pos:vec(-1,-1,-1), color:colorCUB2, opacity:0.5})
let planF1C0 = quad({v0:v0F1, v1:v1F1, v2:v2F1, v3:v3F1})
C0.append(planF1C0)”

--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vpython-users/eaadb22c-074c-4ab6-920e-0b586e007e8an%40googlegroups.com.

EDUCATIA SUPERLATIV

unread,
Mar 5, 2026, 8:20:42 AMMar 5
to VPython-users
The previous code has the mistake that it doesn't take the spaces from Python code when I paste it!!!!!
I don't know why
See the image of the code

Best regards,
Ciprian

John

unread,
Mar 8, 2026, 12:45:02 AMMar 8
to VPython-users

The original error you reported, namely " ReferenceError: Cannot access 'C0' before initialization" is an error related to javascript, it is not an error message you get from a python program. If you are working with web vpython programs then you should probably ask on the web vpython forum. Have a look at the web vpython demo programs here

https://glowscript.org/#/user/GlowScriptDemos/folder/Examples/

click "view" to view the vpython code for any of the demo programs, then click " Share or export this program  " to view how to get a version of the program that can run in an html file. You will see with this last link you get your vpython program automatically translated into javascript so that it can run in an html file.
Reply all
Reply to author
Forward
0 new messages