Python Forum
Why does lambda throw 'name value_o is not defined' error? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why does lambda throw 'name value_o is not defined' error? (/thread-31480.html)



Why does lambda throw 'name value_o is not defined' error? - karabakh - Dec-14-2020

Note that this is happening in codewars and my IDE is not showing any errors.

def decrypt(text,n):
    i=0
    test=""
    if n<0:
        text=text
    else:
        while i<n:
            for index,value in enumerate(text):
                global value_e
                value_e=[value for index, value in enumerate(text) if index<len(text)/2]
                global value_o
                value_o=[value for index, value in enumerate(text) if index>=len(text)/2]
            i+=1
    print(value_o)
    for x in map(lambda x,y : x+y, value_o, value_e):
        test+= x
here's the error:
Traceback (most recent call last):
File "main.py", line 12, in <module>
Test.assert_equals(decrypt("This is a test!", 0), "This is a test!")
File "/home/codewarrior/solution.py", line 14, in decrypt
for x in map(lambda x,y : x+y, value_o, value_e):
NameError: name 'value_o' is not defined


Thank you


RE: Why does lambda throw 'name value_o is not defined' error? - Axel_Erfurt - Dec-14-2020

def decrypt(text,n):
    value_e = []
    value_o = []
    i=0
    test=""
    if n<0:
        text=text
    else:
        while i<n:
            for index,value in enumerate(text):
                value_e=[value for index, value in enumerate(text) if index<len(text)/2]
                value_o=[value for index, value in enumerate(text) if index>=len(text)/2]
            i+=1
    print(value_e)
    print(value_o)
    for x in map(lambda x,y : x+y, value_o, value_e):
        test+= x
decrypt("hello world", 11)

Output:
['h', 'e', 'l', 'l', 'o', ' '] ['w', 'o', 'r', 'l', 'd']



RE: Why does lambda throw 'name value_o is not defined' error? - deanhystad - Dec-14-2020

if n == 0 this code never runs:
            for index,value in enumerate(text):
                global value_e
                value_e=[value for index, value in enumerate(text) if index<len(text)/2]
                global value_o
                value_o=[value for index, value in enumerate(text) if index>=len(text)/2]
If that code doesn't run, value_e and value_o are not defined. You would get the same error running this code in you IDE if the first time you called decrypt you passed n == 0. However if you call decrypt() with n != 0 it will define value_e and value_o in the global scope and a future decrypt() call with n == 0 will will give invalid results but not crash.


RE: Why does lambda throw 'name value_o is not defined' error? - karabakh - Dec-14-2020

(Dec-14-2020, 04:58 PM)deanhystad Wrote: if n == 0 this code never runs:
            for index,value in enumerate(text):
                global value_e
                value_e=[value for index, value in enumerate(text) if index<len(text)/2]
                global value_o
                value_o=[value for index, value in enumerate(text) if index>=len(text)/2]
If that code doesn't run, value_e and value_o are not defined. You would get the same error running this code in you IDE if the first time you called decrypt you passed n == 0. However if you call decrypt() with n != 0 it will define value_e and value_o in the global scope and a future decrypt() call with n == 0 will will give invalid results but not crash.

Now I see where it went wrong. Thanks a lot!