Find the bug!


#1

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
    results = []
    for numbers in lists:
        results = results.append(numbers)
        return results

     print flatten(n)

error message:Oops, try again. flatten([[1, 2], [3, 4]]) returned None, did you remember to return the result?

BUT I HAVE RETURN THE RESULTS


#2

the function call:

print flatten(n)

should be outside the function (0 spaces indention)

then here:

results = results.append(numbers)

append() will manipulate the list directly, and return None. So by saving the result in a variable, you overwrote your list with None. Don't save the result of calling .append()in a variable

finally, a function ends the moment a return keyword is reached (even when the return keyword is reached in a loop), so your loop ends in its first iteration.

Your code won't flatten the list, it will simply create a new list which is identical to n


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.