Home > Net >  Problems running python tests
Problems running python tests

Time:04-14

when I try to run the tests on this code I am having problems with item, I do not understand what can fail

def fix_me(my_list):

if len(my_list) % 2:  # imperative code
    new_list = []
    for item in my_list:
        for element in item:
            new_list = new_list.append(element)
else:  # functional code
    new_list = [element for element in my_list for element in item]

# sorting hierarchy:
#   1. desc by x % 5
#   2. desc by x
return new_list.sort(reverse=True, key=lambda x: x % 5   x/(max(new_list)*2))

Arguments: [ [ 3, 4 ], [ 2, 6 ] ]

output: Error: free variable 'item' referenced before assignment in enclosing scope Tipo de error: NameError Lugar: Error en la linea número 9 (" new_list = [element for element in my_list for element in item]")

CodePudding user response:

The comprehension list has a problem. It's not right "for element in item".From what I understand, you want the second element of the lists. So you have to go to element[1]. e.g new_list = [element[1] for element in my_list]

CodePudding user response:

If your trying to get all the elements into new_list then change

new_list = [element for element in my_list for element in item]

to

new_list = [element for item in my_list for element in item]
  • Related