Modifying each element in a list in a function


#1

I think I already have valid output of this exercise.However, there's an error still.

error:
Oops, try again. double_list([0, 1]) returned None. Did you remember to return the result?

May anybody have a look for me, please? ><
Thanks.

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

#also it's unavailable to be written like below:

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

#also I know it works as below:
n = [3, 5, 7] 
def double_list(n):    
    for i in range(0, len(n)):         
        n[i] = n[i] * 2
    return n 
print (double_list(n))


#what's the different logic behind this three codes?
# Are we unable to calculate behind "Return" function?


#2

Which of the three is yours?

First issue,

return print n? Doesn't seem like proper syntax :smile:


#3

According to the exercise, your function should return a value.

Make sure you know what print and return are, what they do, and when and how you should use them.

HTH.


#4

these all three are coming out of my brian :joy::joy::joy:

btw, so we DON'T have any syntax like

RETURN PRINT (XXXXXX)

right?

thank you :slight_smile:


#5

thank you for ur response :smile:

I think what makes me confuse is that:

The exercise asks me to RETURN the value, not only calculate the correct answer.

However, if in real cases, the first code and the third code will have the same use for circumstances like this. Am i right?


#6

In brief, a print statement simply “prints’ out a line in the console.

>>> print "Hello, World!"
Hello, World!
>>>

A return statement causes a function to exit and return or give a value back to the caller of the function.

>>> def add_one(x):
       return x + 1

>>> val = add_one(10) # caller of the function
>>> print "add_one returned " + str(val)
add_one returned 11
>>>

#7

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