Write a function named
append_sum that has one parameter — a list named named
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
For example, if
lst started as
[1, 1, 2] , the final result should be
[1, 1, 2, 3, 5, 8] .
if(len(lst) >= 6):
So obviously I don’t have to answer the question this way, but I saw that maybe it can be done with recursion, plus also trying to learn recursion in practice. So it might be that my understanding of recursion is still not on point, but also because python is this weird alien language to me.
So my problem is most likely with the else statement. Originally if the else statement was a single line of:
lst.append(list[-2] + lst[-1])
the code runs fine-ish. It returns None. Which I’m also trying to wrap my head around. But after adding append_sum(lst), I get:
Traceback (most recent call last):
File “script.py”, line 11, in
print(append_sum([1, 1, 2])) //print test curtesy of the challenge question
File “script.py”, line 7, in append_sum
TypeError: ‘int’ object is not iterable
For me, the important thing is that I understand the code. So it’d be helpful if someone could explain both the “None” result and the current “TypeError” to me, and how I can improve on/fix the recursion.