python - SyntaxError: name 'variable' is used prior to global declaration -


im writing script here:

http://www.codeskulptor.org/#user40_ouvcoj2dj1_8.py

my fault lies in code:

if 'i' not in globals():     global     if 'j' in globals():         = j     else:         = 0 

i want assign i j if j exist in global scope. if j doesn't exist i begins @ 0. , j might globally declared later in script, if input right.

you run script pressing play in top left.

this not how global variables work in python. if i'm guessing intent correctly, want code:

if 'i' not in globals():     global 

to interpreted "if there's not global variable named i, create global variable name." that's not code says (and written, doesn't make sense). closest translation of code like:

if there's no global variable named i, when attempt use variable i in scope, i'm referring global i (which doesn't exist) instead of creating new variable i exists inside current scope.

global never creates anything, tells interpreter you're referring to.

some possibly useful links:

https://docs.python.org/2/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python

https://infohost.nmt.edu/tcc/help/pubs/python/web/global-statement.html


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -