Modifying each element in a list in a function


#1



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


Oops, try again. double_list([0, 1]) returned [0, 1] instead of [0, 2]
CURRENTLY RECEIVING:
[6, 5, 7]
None


Do not understand how my code is not working, even though I see forum posts with "solved" written on them, despite having the same code as me. While technically cheating, I have been stuck too long on this section for me to care. Need support.

n = [3, 5, 7]

def double_list(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
        return x


print double_list(n).


#2

Stick in some print statements where you print out what is happening step by step


#3

Got this when I tried doing this

n = [3, 5, 7]

def double_list(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
print x
return x

Oops, try again. double_list([0, 1]) returned [0, 1] instead of [0, 2]


#4

Note that what you posted will not run. Ensure that code is intact when you share it, otherwise you shared something other than the code you're talking about - defeating the purpose of sharing the code.

Call your function and observe what it prints to see what it's doing. If you need more information, add more / more detailed prints. Right now it appears that you don't call it at all, so you also will not get any information from your prints due to not running them.


#5

Yep, just solved it, still confused how though. The code looks the exact same to me.

n = [3, 5, 7]

def double_list(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
return x

print double_list(n)


#6

Compare to what you posted here. Also consider what you should have done to find it (sounds like you just changed things with no plan) - instead you should be identifying what it's doing differently from what it should do (for example by printing a log of what's being done) and then modifying the code that is responsible for that behaviour (possibly learning more about that part first)


#7

from another user in another thread:
a function ends the moment a return keyword is reached, if a return keyword is reached in a loop, the loop will break, your loop breaks in its first iteration

In your case, the "return" is inside your for loop instead of just being inside the function.


#8

n = [3, 5, 7]

def double_list(x):
for i in range(0,len(x)):
x[i] = x[i] * 2
return x

print double_list(n)

Definitely works....just need the return function to be outside the for loop so that the loop can itterate


#9

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