Python code challenges-lists


I have tried the below code and i am getting the partial output .Please check the code and tell me where i have done the mistake.

**CODE **


#Write your function here

def append_sum(lst):

sum = lst[-1]+lst[-2] #1+2=3


return lst #should return [1,1,2,3]

new =sum+lst[-2] #3+2=5


return lst #should return [1,1,2,3,5]

num= new+lst[-2] #5+3=8


return lst #should return [1,1,2,3,5,8]

print(append_sum([1, 1, 2]))

Hey there @saisrijaedara7486017!! :grinning:

It is hard to understand your code without the indentation, could you format it using the button at the top of the post menu </>?

Just from looking at it like this it seems you may not fully understand how to use return.
return is used to send value back from a function, it seems you already now this.

def add(m, n):
  new_value = m + n
  return new_value

print(add(5, 6))  #will print 11 because of return statement

However each function can only return once, after it returns the function ends.

def add(a, b, c):
  new_value = a + b
  return new_value #function ends here
  new_value += c
  return new_value #this code will never be run

print(add(1, 2, 5)) #prints 3

Your code has three return statements but only one function, as soon as the code reaches the first return statement it will end the function and your code stops.