10.Modifying an element of a list in a function


#1



HELP MEEEEEE


Here is the Error Message: Oops, try again. list_function([5, 0]) returned 0 instead of [5, 3]


def list_function(x):
    return x[1]

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

Okay... I gotta go :slight_smile: Please help me while I'm gone :wink:


#3

You over-complicated the problem here buddy.
The instructions are as follows:

Change list_function so that:

01. Add 3 to the item at index one of the list.
02. Store the result back into index one.
03. Return the list.

First, they only want you to make one function called list_function(x) which is what you have but no more. I'm not sure where double_list(x) comes from.
Second, they want you to add 3 to index one. Meaning: x[1] + 3. Not multiply by 2.
Third, store the result back to index one. I believe you know how to store variables back to each other. Eg. dog = dog + bark.
Last, return list (which is your parameter).

And that's it! Not need for two functions. No need for multiplication, loops, or any fancy stuff like that. Should be a really short code :slight_smile:


#4

... i made my code different (a bit) and then i read this... Here is my code :slight_smile:

def list_function(x):
    return x[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)

#5

But it still doesn't work :frowning:

def list_function(x):
    return x[1]

n = [3, 5, 7]
def list_function(x):
    n = n + x
print list_function

#6

Like I said before, you don't need two functions. Just one is enough as specified in the instructions of the lesson.

You currently still have two functions with the exact same name. Why not combine them? Also, look a little closer to my previous comment. I gave you more hints. Let me know anything that you don't quite understand :slight_smile:


#7

I was stuck for three days in a row, obviously there were other distractions too in the surrounding, but your help was like a magic. I couldn't help myself from thanking you for your suggestion. Glad, that I've come across the post.


#8

Still stuck...

def list_function(x):
    return x[1]

n = [3, 5, 7]
def list_function(x):
    n = n + x
print list_function

#9

You still have 2 functions. You only need 1 function. One def list_function. Remove the second one.
Next, they want you to add 3 to index 1. Not add x to n. Should look like this:
x[1] is index 1 of the list. Add 3 to it. NOT anything else (like the x and n you have there).
And make sure to return x since that is your parameter that you want to output.

If you don't understand something, please be more specific about what you don't understand :slight_smile:
I suggest you re-read the instructions as well. Perhaps it will make things more clear.


#11

But how
IF i do this

def list_function(x):
    return x[1]

n = [3, 5, 7]

    n = n + x
print list_function

And it doesn't work


#12

Need to add 3 to x[1]. And remove the n = n+x. I'm not sure what that's supposed to do or why you have it there.
Don't just print the function name (it's incomplete). Do print list_function(n).


#13

... :confused: ... ... I don't get it :confused:


#14

Made changes to comment above.


#15

def list_function(x):
    return x[1]

n = [3, 5, 7]

s = n + x
print list_function(s)

Shrugs IRL


#16

When modifying an element to the list, you need to get the element in the list to modify it. To do that, for example say you have a list

n = [1, 2, 3, 4]

and you want to modify index 1 of the list by adding 1 to it. The desired output should be

[1, 3, 3, 4]

How you modify it is

n[1] = n[1] + 1

as indexes start at 0 and n[1] means to get index 1 of the list.
This is just an example to what you're trying to do here.
If you don't remember how functions work, I suggest going back and reviewing it.


#17

def list_function(x):
    return x[1]
s = [0,0,0,0]
p = [2,1]
n = [3, 5, 7]
n = n + p
print list_function(n)

What is wrong here :frowning:


#18

Remember, I'm really bad at this :slight_smile:


#19

Nevermind, I figured it out :stuck_out_tongue:


#20

I think the key is to keep it as simple as possible, don't overcomplicate things.
Here is my code:

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

n = [3, 5, 7]

print list_function(n)


#21

You got the instructions wrong. In the exercise we are meant to:

Create a function called double_list that takes a single argument x (which will be a list) and multiplies each element by 2 and returns that list. Use the existing code as a scaffold.