Trouble with the for loop


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/3/2?curriculum_id=4f89dab3d788890003000096


I expected the function double_list(n) to return the list n with all of its values multiplied by two. It returned them as they were originally.


def double_list(x):
    for item in x:
        item *= 2
    return x
    
print double_list(n)


#2

Above that code is also included this line:
n = [3, 5, 7]
but it still doesn't work.


#3

This has no effect on the list.

Consider going over the list and caching the result in another, and return that.

Alternately, you could use range() and the index to modify the list element in place.

Edit:

This unit is quite a ways into the track, and we are dealing with lists and functions so it is fair to review one important built-in list method, list.index().

Using this method it is possible to work with the original code and item lookup.

        x[x.index(item)] *= 2

#4

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