Python Forum
Why does lambda throw 'name value_o is not defined' error?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does lambda throw 'name value_o is not defined' error?
#1
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
Reply
#2
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']
Reply
#3
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.
karabakh likes this post
Reply
#4
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 569 Nov-23-2023, 02:53 PM
Last Post: rob101
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,118 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Error 'Contour' not Defined DaveG 3 2,331 Mar-13-2022, 03:29 AM
Last Post: deanhystad
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 3,588 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,369 Apr-13-2021, 07:22 PM
Last Post: HeRo
  name error "name"is not defined MaartenRo 1 3,417 Jul-28-2020, 02:39 AM
Last Post: bowlofred
  Name Error: name 'Stockton' is not defined Pinokchu 3 2,267 Jun-13-2020, 02:48 PM
Last Post: Yoriz
  python library not defined in user defined function johnEmScott 2 3,824 May-30-2020, 04:14 AM
Last Post: DT2000
  error ,,name append is not defined'' Killdoz 1 5,019 May-24-2020, 06:23 PM
Last Post: bowlofred
  Lambda function recursion error DeadlySocks 1 2,043 Apr-13-2020, 05:09 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020