13. modifying each element : it works but still meh


#1



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


i dont understand why the printed output is right but still getting the error :


n = [3, 5, 7]
def double_list(x):    
    for i in range(0, len(n)):
        n[i] = n[i] * 2

# Don't forget to return your new list!
    return n
    n[i] = x
print double_list(x)

any valid explanation?? Thanks guys!!


#2

You are close, but maybe this outline will help you:

n = [3, 5, 7]

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

print FUNCTION_NAME(n)

#3

Thank you @acquaphoenix17 for your reply.
My question prob wasnt clear enough:
i succesfull passed that exercise, but still dont understand why i get the error while on the command line the result is right. Is this a sort of bug ? thats what i dont get..
Im referring to the "returned[12,20,28]


#4

This isn't a bug, you get an error because you're code is not entirely correct. You're code only works if n is the argument for double_list(). Codecademy's SCT checks to see if your code would work even if n wasn't the argument being passed. Since your code does not work in this case, you get an error.


#5

your function should work for more then 1 list, functions that work for just a single list is not really great. Look:

n = [3, 5, 7]
def double_list(x):    
    for i in range(0, len(n)):
        n[i] = n[i] * 2

# Don't forget to return your new list!
    return n
    n[i] = x
print double_list(x)
print double_list([6,8,10])
print double_list([1,2])
print double_list([9,6,3,0])

your function should work for all this lists (and more) (the exercise also calls the function multiply times with different lists for validation purpose, that is where [12,20,28] comes from)

to make your function work for all this lists you supply as arguments, use the function parameter (x) inside the function instead of n

(the argument you supply at function call, gets copied into the function parameter)

The great things about functions is that we can use them multiply times, by simply calling the function again and supplying a different argument.


#6

i don't understand what you did here
n[i]=x


#7

nobody did this? It would assign x to index i in the list n


#8

I think, this is a correct answer:

n = [3, 5, 7]

def double_list(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
# Don't forget to return your new list!
    return x

@aquaphoenix17 explained it very well - parameter in the loop "for" should be the same as in the function. @microsolver10466, when you use there parameter n, it works only for n (given in the first line).

It prints [6, 10, 14], because you wrote print double_list(x) - where x = n[i], but still return result only for n (it must work for any values).

Take a look at your error: double_list([0, 1]) returned [12, 20, 28] instead of [0, 2].

I hope that I helped.


#9

define your function first, forget about existing n list in case you get confuse:
this function should work with "any" list, not only n list, that why we take x as argument

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

and then you add on, which finally assign n--->x

n = [3, 5, 7]
print double_list(n)

if you have other list like a = [ 2, 4, 6]
you assign a --->x

a = [2, 4, 6]
print double_list(a)

hope this help :slight_smile:


#10

i was just trying to be even more sure to pass the var n[i] into x, altought i know its not necessary :stuck_out_tongue:


#11

thanks for the complete explanation guys!! (Y)


#12

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