0

New Python guy here, kinda pulling my hair out on this one. Running Python 3.6, Windows... I know "NameError: name '--' is not defined" has plenty of posts, but I honestly haven't been able to resolve this from any of them, so pardon any repetitiousness. ANYway, here's the trouble:

def char_swap():
    swapped_a = [l.replace('a', '@') for l in lines]
    swapped_s = [l.replace('s', '$') for l in lines]
    swapped_i = [l.replace('i', '!') for l in lines]
    swapped_o = [l.replace('o', '0') for 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))) for n in swapped]
    print(added)
    return added

Here, 'lines' is a previous list within the full code. The error is "NameError: name 'swapped' is not defined"

What's almost more boggling to me is that the 'char_swap' function is working just fine, and it's referencing 'lines', which was setup and defined pretty much verbatim how I've defined 'swapped' here. Global, gave it a list value, and off we go. In fact, if I swap out 'swapped' in the numberAdd() function for 'lines', it does its job just fine.

Any help is much appreciated!

lo_pass
  • 21
  • 3
  • 1
    did you define swapped outside of char_swap too? – ingvar Sep 10 '17 at 18:12
  • 1
    Possible duplicate of [Using global variables in a function other than the one that created them](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – PRMoureu Sep 10 '17 at 18:12
  • 3
    Please provide an **actual** [mcve], that produces the error message; there are now 2 function definitions, but no example on how you're calling them; also any Python question about an error should include the *complete* `Traceback` starting from that line and ending to the `xxxError:....` – Antti Haapala -- Слава Україні Sep 10 '17 at 18:15
  • Are you assigning the result when you call `char_swap()` ? since you return `swapped` maybe you can pass it to `numberAdd` by declaring this function like this : `def numberAdd(swapped):`. This can avoid the use of a global variable here – PRMoureu Sep 10 '17 at 18:15
  • @ingvar no, but wouldn't that be rendered unnecessary by making it global? If I'm understanding you correctly.. Or do you mean, in addition to it's definition within char_swap? – lo_pass Sep 10 '17 at 18:15
  • @PRMoureu I disagree with trying to choose a duplicate when the question is so unclear. – o11c Sep 10 '17 at 18:39
  • @o11c i agree, the problem could be different, but it looks obvious. it's a possible duplicate, not mentioned to punish the OP, but a lead to help without repeating the same things – PRMoureu Sep 10 '17 at 18:47

1 Answers1

1

In the first function char_swap, you have defined the global variable swapped. In the second function number_add, you are using the global variable swapped. The problem is, the global variable swapped is only created when you run char_swap, not when you just define it.

The problem, and the cure, can be illustrated by this simple example:


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

The first time we execute g, before executing f, python doesn't know about z. After executing f, then python knows about z so g executes properly.