Function produces no results when trying to append to a list

I am unable to produce any results with the following question:

Write a function named append_sum that has one parameter — a list named named lst . The function should add the last two elements of lst together and append the result to lst . It should do this process three times and then return lst . For example, if lst started as [1, 1, 2] , the final result should be [1, 1, 2, 3, 5, 8] .

Here is my code:

#Write your function here

def append_sum(lst):
  a = lst[-1] + lst[-2]
  lst = lst.append(a)
  return lst

#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))

I know I am not producing the results three times here but I would think this would return something. Instead it returns “None”.

.append() updates the list in place/memory. But the append method itself doesn’t return anything (None), which you then use to overwrite your lst

6 Likes

@stetim94

Understood, I’ve updated my code below to remove the overwrite (or at least I think) and it is still returning “None”

#Write your function here

def append_sum(lst):
  a = lst[-1] + lst[-2]
  return lst.append(a)

#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))

But the append method itself doesn’t return anything (None)

That statement remains true.

If the function’s goal is to return a list with an appended_sum, you should return only the list.

If the function’s goal is to just append the sum, the you don’t need to return anything, and if you print the list after invoking append_sum on it you will see the effect that has taken place.

2 Likes

@toastedpitabread

I think I see what you’re saying. My goal is to just append the sum and then print out the results when calling the function.

#Write your function here

def append_sum(lst):
  a = lst[-1] + lst[-2]
  lst.append(a)

#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))

My other confusion comes from nothing being displayed from my print. There is no error in the code, so I would expect to see some type of result returned.

A function is like a machine, it does something for you. If it doesn’t return a statement, it normally isn’t useful to print the function call itself.

Consider some variations:

def append_sum(lst):
  a = lst[-1] + lst[-2]
  lst.append(a)

test = [1, 1, 2]
append_sum(test) #sum of 1 and 2 (3) is appended to test
print(test)
# [1, 1, 2, 3]

def append_sum(lst):
  a = lst[-1] + lst[-2]
  lst.append(a)
  return lst

print(append_sum([1, 1, 2]))
# [1, 1, 2, 3]
# but it's just a print, no values are stored. To store somewhere:
new_list = append_sum([1, 1, 2])
print(new_list)
# [1, 1, 2, 3]

Considering the name of the function, I’d go with the first variation myself (not in regards to the exercise, with that you have to follow the instructions to pass).
The difference being the first function appends the sum to the list, whereas the second returns a new list with an appended sum. Maybe it’s just me though.

2 Likes

@toastedpitabread

I guess the exercise is throwing me off then. From the directions it seems like I just need to write a single function that will print the results from

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

I shouldn’t have to define a variable outside of the function, based on the directions.

#Write your function here

def append_sum(lst):
  a = lst[-1] + lst[-2]
  lst.append(a)
  return lst
  
#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))

I think I’m on the right track now however.

Yes! I’m just saying in your own projects when you write functions you want them named in a way that gives a very good clue to what they do (within reason).

1 Like

@toastedpitabread

Awesome! Here is my final code with a better variable name

#Write your function here

def append_sum(lst):
  combine_last_two = lst[-1] + lst[-2]
  lst.append(combine_last_two)
  combine_last_two = lst[-1] + lst[-2]
  lst.append(combine_last_two)
  combine_last_two = lst[-1] + lst[-2]
  lst.append(combine_last_two)
  return lst

#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))

This does make it easier to read and understand vs. just assigning random variable names like x and y. The funny thing is, in school and online courses - you are taught variables as single letters most times.

1 Like

I think with text editors now auto-completing, I’m all for more readability (as long as the name doesn’t get too long).

If the code gets large it gets hard to maintain when you come back to it after time otherwise.

1 Like

One of the biggest principles in programming is the DRY principle which stands for Do Not Repeat Yourself. Instead of writing out the procedure three times, the same thing can be achieved by employing for loop and range function. You can specify the number of times you want the procedure to be repeated in range function and use a for loop to loop over the procedure. The underscore in my code denotes the unused variable.

def append_sum(lst):
    for _ in range(3):
        lst.append(lst[-1] + lst[-2])
    return lst

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

range(3) should probably be range(len(list)). Otherwise it won’t work on any other length of list.

1 Like

Depends. If you simple want to execute something 3 times, range(3) is good

this code misses a check to verify there at least two elements in the list (and if lst is even a list). Otherwise you might get errors.

2 Likes

True