Python NameError: name '' is not defined (though it sure seems like it is) -


new python guy here, kinda pulling hair out on one. running python 3.6, windows... know "nameerror: name '--' not defined" has plenty of posts, haven't been able resolve of them, pardon repetitiousness. anyway, here's trouble:

def char_swap():     swapped_a = [l.replace('a', '@') l in lines]     swapped_s = [l.replace('s', '$') l in lines]     swapped_i = [l.replace('i', '!') l in lines]     swapped_o = [l.replace('o', '0') l in lines]     global swapped     swapped = [swapped_i,swapped_o,swapped_s,swapped_a]     print(swapped)     return swapped  def numberadd():     added = [n+(str(random.randint(0,9999))) n in swapped]     print(added)     return added 

here, 'lines' previous list within full code. error "nameerror: name 'swapped' not defined"

what's more boggling me 'char_swap' function working fine, , it's referencing 'lines', setup , defined pretty verbatim how i've defined 'swapped' here. global, gave list value, , off go. in fact, if swap out 'swapped' in numberadd() function 'lines', job fine.

any appreciated!

in first function char_swap, have defined global variable swapped. in second function number_add, using global variable swapped. problem is, global variable swapped created when run char_swap, not when define it.

the problem, , cure, can illustrated simple example:

     def f():         global z         z = 17
def g(): return z
> g() nameerror: name 'z' not defined > f() > g() 17

the first time execute g, before executing f, python doesn't know z. after executing f, python knows z g executes properly.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -