List and Functions


#1
n = [3, 5, 7]
def double_list(x):
  for i in range(0, len(n)):
    x[i] =x[i] * 2  
    return x[i] # x should be used.
print(double_list(n)) 

expected result: 6,10,14
printed result: 6 :frowning: Help ?


#2

Check the indentation on your return line.


#3

Sir If I print x instead of x[i] it will print result in list form e.g: [6,10,14]. I am bit confused what is different between returning x[i] and x since both shows same output but in different way.


#4

The function should modify the entire list and then return it. x[i] is only one element. x is the entire list.

Your code above has the return line inside the loop so it exits the loop on the first iteration.


#5

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