You’ve created an infinite loop. i
will always be less than len(lst)
since you append to lst
with each iteration of your while
loop. You don’t need an index variable i
. Try while len(lst) < 51
if you want to return a list with 50 elements. Hope this helps!
def append_sum(lst):
while len(lst) < 6:
sum = lst[-1] + lst[-2]
lst.append(sum)
return lst
print(append_sum([1, 1, 2]))
[1, 1, 2, 3]
I’m still unsure what I’m doing wrong here, sorry to keep this thread open. Is it possible to make these lines of code work? My goal here is to have a list with a length of at least 6 elements made from adding the last two elements of the lst
together. Then appending that value into lst
[1, 1, 2, 3, 5, 8]
My goal is to have the code print this
Your code is correct with one small (BIG in Python) error. Your return lst
statement is indented the same as the code in your while
loop making it part of the code body of the loop. Once that line is reached, the value of lst
is returned to the caller terminating the execution of the function. If you out-dent the return
statement one level keeping inside the append_sum
function, but removing it from the while
loop, you’ll have what you’re looking for.
Hello, @raj_chiranjeevi! Welcome to the forum.
Please see this answer: FAQ: Code Challenge: Lists - Append Sum
You have the exact same issue.
#Write your function here
def append_sum(lst):
add = lst[-1] + lst[-2]
lst.append(add)
add = lst[-1] + lst[-2]
lst.append(add)
add = lst[-1] + lst[-2]
lst.append(add)
return lst
#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))
Why do I have to return lst
in the last line ?
As far as I know lst.append(add)
already change the data of list and we don’t need to return.
I can’t understand when do I have to return or not.
What does it mean to return the value?
Hello, @jbjang3822260770! Welcome to the forum.
In order for print(append_sum([1, 1, 2]))
to actually print anything the value has to be returned by the function append_sum()
. Without returning the value, None
would be printed instead.
Side note, your function should use some type of loop to repeat the action instead of hard coding the exact same operation 3 times. Always try to DRY your code. (Don’t Repeat Yourself)
Hello, thanks for your reply.
But what doest it mean to return ?
I still can’t understand it.
lst.append(add) already make new lst but why I have to return it?
For example, I have also trouble below.
#Write your function here
def append_size(lst):
new_list = lst.append(len(lst))
return new_list
#Uncomment the line below when your function is done
print(append_size([23, 42, 108]))
It shows None. But why?
append_sum([1, 1, 2])
This statement executes the function. If you did this without the print()
function, your append_sum()
function would run its code, and no one would ever know that anything had happened. The list [1, 1, 2]
would become [1, 1, 2, 3, 5, 8]
, and then disappear into the abyss. The return
statement is how we are able to carry out further action or save the value generated by a function. In your code, return lst
sends the modified list [1, 1, 2, 3, 5, 8]
back to the line of code that called the function referred to as the ‘caller’. In your case the caller is: print(append_sum([1, 1, 2]))
, so the modified list is returned, and then that value is used as the argument for the print()
function. This results in [1, 1, 2, 3, 5, 8]
being printed in the console. When a function lacks a return
statement, None
is implicitly returned, so the caller knows the function has finished executing.
Consider these examples:
#example without return:
def first_and_last(first_name, last_name):
name = first_name + ' ' + last_name
print(first_and_last('John', 'Smith'))
#output: None
#example with return:
def first_and_last(first_name, last_name):
name = first_name + ' ' + last_name
return name
print(first_and_last('John', 'Smith'))
#output: John Smith
Hope this helps!
Okay, this explanation is a bit more complicated. lst.append(len(lst))
doesn’t return a new list. The .append()
method modifies the list that it is called on, but doesn’t return anything, so new_list
is being assigned None
. Your function would work as desired by omitting the new_list
variable like so:
def append_size(lst):
lst.append(len(lst))
return lst
Thanks a lot !
Now I understand the concept of ‘caller’ and why it is needed to save the value generated by function.
I’ll keep going my courses for further understanding !
Hey all,
First time posting; no prior coding experience to Codeacademy, and Python is my first course, so you’ll probably learn to roll your eyes at me.
Having said that, this whole list section is twisting my mind, and I do plan on retaking it once I struggle my way through.
I simply need to know:
The solution is given as
def append_sum(lst):
lst.append(lst[-1] + lst[-2])
lst.append(lst[-1] + lst[-2])
lst.append(lst[-1] + lst[-2])
return lst
I understand that logic. As the list is appended, -1 and -2 pull different elements as the list increases. Great. No problem.
What I cannot understand for the life of me is why my code did not do the same thing. It is messier, but the logic, in my own mind, is identical:
def append_sum(lst):
sum1 = lst[-2] + lst[-1]
lst.append(sum1)
sum2 = lst[-2] + lst[-1]
lst.append(sum2)
sum3 = lst[-2] = lst[-1]
lst.append(sum3)
return lst
I am appending to the list all three times. The list, once appended, should stay appended. As such, the -2 and -1 should be changing for each new sum I make. However, the answer returned was:
[1,1,2,5,5,5]
Why did it repeat the same math with the same values, even after the list was appended? It clearly did append, as the list is longer. Why did -1 not pull 5 in the last two attempts?
Thank you in advance for the help. I realize this is probably basic stuff to everyone, but I’m training my mind to think in code, and part of that is disabusing it of the “normal” logical leaps I make in this context.
At a glance I don’t see a difference, easier if you post code where other people can press “run” and get the same thing to happen with no modifications whatsoever. Otherwise effort goes to editing and guessing instead of the problem.
Copied from your post, indented and adding an invocation to your function:
def append_sum(lst):
sum1 = lst[-2] + lst[-1]
lst.append(sum1)
sum2 = lst[-2] + lst[-1]
lst.append(sum2)
sum3 = lst[-2] = lst[-1]
lst.append(sum3)
return lst
print(append_sum([1, 1]))
This prints:
[1, 1, 3, 3, 3]
…Doesn’t look like we’re running the same code.
Maybe the confusion comes more from mixing up different versions of your code, rather than the code itself?
Hello, @sipeelnino! Welcome to the forum!
Prior to pasting your code into a post, please click on the </>
button. You’ll find it in the menu bar at the top of text area you type in. Then you can past your code in the space indicated, and the formatting including indentation which is very important in Python will be preserved. It also allows for others to simply copy your code, and paste it into their own environment where we can run it, and see what happens. Thanks!
I was able to duplicate your output with the following:
def append_sum(lst):
sum1 = lst[-2] + lst[-1]
lst.append(sum1)
sum2 = lst[-2] + lst[-1]
lst.append(sum2)
sum3 = lst[-2] = lst[-1] #oops, I think one of those should be a '+'
lst.append(sum3)
return lst
print(append_sum([1, 1, 2]))
You have a typo in the line indicated by my comment to your code. Happy coding!
Using a for loop with a range of 3, I repeated the process of adding the last 2 items of the list. Whenever you notice that your function is writing the same line of code several times, it usually means it is a good target for a loop
def append_sum(lst):
for i in range(3):
lst.append(lst[-1]+lst[-2])
return lst
… or even a function.
But that would be stretching it for this one. If this is a one-off function, then it can all be expressed in the return value.
>>> def append_sum(x):
return x + [x[-2] + x[-1]]
>>> a = [0, 1]
>>> a = append_sum(a)
>>> a = append_sum(a)
>>> a = append_sum(a)
>>> a = append_sum(a)
>>> a = append_sum(a)
>>> a
[0, 1, 1, 2, 3, 5, 8]
>>>
It’s useful to ensure that lst
has at least two elements before proceeding:
def append_sum(lst):
assert(len(lst) >= 2)
for _ in range(3):
x = lst[-2] + lst[-1]
lst.append(x)
return lst
Here’s how I tackled it. Any thoughts?
def append_sum(lst):
n = 3
while n > 0:
result = sum(lst[-2:])
lst.append(result)
n = n-1
return lst