Modifying an element of a list in a function


#1

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

ERROR: Oops, try again. list_function([6, 0]) returned [9, 3] instead of [6, 3]

can anyone help on this ?


#2

Yes I can.

The name of the function should be double_list

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

#3

Hi bayoishoal20,

Still i am getting error as:

Oops, try again. list_function([2, 6]) resulted in an error: list assignment index out of range

Regards,
Prasanth.c


#4

I just did using the same code

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

The output is:

[6, 10, 14]
None

Please post a screenshot.

Thank you! :slight_smile:


#5

Once again, you have probably posted code from the wrong lesson. Please don't answer if you are not sure what lesson the OP is working on. Read the error message carefully. The SCT is running list_function to test the code, not double_list. That clue should be screaming out at you.


#6

Without any link to the lesson we are left guessing what the assignment is. This problem as evidenced in the error message looks to be adding 3 to a single element.

list_function([6, 0]) returned [9, 3] instead of [6, 3]

def list_function (x):
    return x[1] + 3

Please post a link to lessons when asking for help. Thank you.


#7

Jeez! I don't even know how that happened. Didn't pay attention to exact function name.

My apologies @mtf & @prasanth_chavana . :slight_smile:


#8

Hi all,

My apologies to everyone. I had raised a wrong question on wrong topic. My actual topic is on "
Modifying an element of a list in a function." Now it is get resolved.

Thanks to @mtf and @bayoishola20

Regards,
Prasanth.C


#9

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