13. Modifying each element in a list in a function


#1



Will someone help me?
Just asking :slight_smile:

I'm really bad at syntax errors sorry :slight_smile:

Oops, try again. Your code looks a bit off. Check the console window for errors!

  File "python", line 2
    def double_list[x]
                   ^
SyntaxError: invalid syntax


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!

print double_list(n)


Help :rage:


#2

Maybe a review on Python's syntax would be very useful to you. If you know you're bad at it, get better. Syntax is really easy to learn. Especially when to use the different brackets:

() or []

Also, I remember telling you that indentation is important. If something is inside something else, it needs to be indented. (HINT: your for loop is INSIDE your function and your expression is INSIDE your for loop, right?)


#3

Hmm.. new code still doesn't work :frowning:

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

#4

Frustrated as ANYTHING!!!!!!!!!


#5

What happened to your function definition def double_list(x)? You seem to be calling it in your print statement (and other places...) but you never defined it.

Not sure why there's a double_list(x) below n[i] = n[i] * 2. Why call the function there?

Remember, n is just an example list. double_list takes in x, a random variable in which you can replace to n in your examples/tests. So everything IN the function need to deal with x and not n.

Why return a function name? Aren't you trying to output the list with every number doubled? Why not return the list variable x?


#6

I am STILL frustrated HERE IS MY CODE:

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 double_list(x)
print double_list(x)

#7

You didn't do those. Or you don't understand what I'm saying?


#8

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