Modifying an element


#1



Hi there

Why is this multiplying and not (as shown in the example!) copying the number? And why is everybody suddenly using for in range (when looking at other questions in Q&A)?:sweat:

I get [6, 5, 7]..

the example for the exercise was:def double_first(n):
n[0] = n[0] * 2

numbers = [1, 2, 3, 4]
double_first(numbers)
print numbers


def list_function(x):
    x[0] = x[0] * 2
    return x

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


#2

are you at Modifying an element of a list in a function? this exercise?

Exactly which exercise are you at? Please copy paste the url of the exercise to the forum


#3

seems like you are, this exercise asks you to:
Add 3 to the item at index one of the list
you seem to multiply the item at index 0. Which is not what the exercise asked of you. No one is using range for this question, that is later


#4

exactly. ok what is the example for then? I tought I could apply this to my code.


#5

Ok, never mind. I understood it know.. thanks!!


#6

No, the example roughly shows how something is done. If it exactly what needs to be done the exercise, you would just be copying it. So the example is to help you in the right direction, and then you have to think how to apply the explanation into your coding problem