Why .append not just adding in the excercise "Append Sum"?

Hello,
I’m completely new here and I’m just starting my journey with programming, that’s why my doubts may be funny sometimes.

In the excercise Append Sum I wanted to make up another way of the function:

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

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

The error which appears is: TypeError: ‘int’ object is not iterable

What is wrong in my way of thinking? I tried also with int(lst[1]) and so on, but I cannot understand, why it is not good.

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-lists/lessons/python-functions-lists-cc/exercises/append-sum

Greetings

This actually gets into some interesting material! Let’s add some print() statements:

def append_sum(lst):
  print("0", lst)
  lst=lst[-1]+lst[-2]
  print("1", lst)
  lst=lst[-1]+lst[-2]
  print("2", lst)
  lst=lst[-1]+lst[-2]
  return lst

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

The output:

0 [1, 1, 2]
1 3
Traceback (most recent call last):
  File "script.py", line 21, in <module>
    print(append_sum([1, 1, 2]))
  File "script.py", line 15, in append_sum
    lst=lst[-1]+lst[-2]
TypeError: 'int' object is not subscriptable

So, going in (step “0”), lst is [1, 1, 2], the value passed to the parameter lst by the calling expression, as you’d expect.

Then it hits your first line of code, lst=lst[-1]+lst[-2] . This is an assignment statement. Every assignment statement does two things:

  1. Evaluate the expression on the right side of the assignment operator (=).
  • In this case, that means evaluating lst[-1] + lst[-2], or 2 + 1, yielding the value 3. So far, so good.
  1. Assign the value thus returned to the variable on the left side of the assignment operator.
  • Ah! Here there be dragons! Until now, the variable name lst has, as is appropriate, referred to the argument assigned to the parameter. That’s what a parameter is, after all: a name to tell you what variable name the argument will have within the function body.

But a variable assignment within the function overrides the parameter name. And here you are taking the value 3 and assigning it to the variable lst. From that moment forward, inside of the function, the list that was assigned to the parameter name lst is gone, and lst is simply the integer 3. This is shown by the output of the second print() statement: print("1", lst) ==> 1 3

So, when the next line of code comes around, lst=lst[-1]+lst[-2], that first stage of the assignment process, evaluating the expression on the right, sees something like3[-1] + 3[-2] and throws: TypeError: 'int' object is not subscriptable


So, that’s why the assignment asked you to use a list method, append(), which would preserve the identity of the object named lst within the function.

4 Likes

Ooooh, now I can see my mistake, thank you so much! :slight_smile:

1 Like

If we use the loop, things become easier but in the question, it is not specified to use the loops hence an ideal solution will be as mentioned below.

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

Can you please explain by a code sample how to construct a condition logic for the loop in this example?

Hello @yazeedsr2561435112, welcome to the forums! Do you mean how would you solve this problem with a loop?
If so:
To solve it with a loop, you have two choices, you could use for with range, or you could use while. Using for with range:

for i in range(3):#This loops through three times-i is 0, 1 and 2
   #code relating to what you would do. See how you would solve it, and change it
#to fit a loop

If you wanted while:

count = 0#declaring the variable to increment (to make it loop thrice
#and to stop an infinite loop:
while count < 3:#This loops three time, count = 0, 1, 2
   #code relating to what you need to do. This would be the same as the code inside
#the for loop
   count += 1#This a) avoids an infinite loop, and b) makes this loop
#iterate over the code 3 times

I hope this helps!

2 Likes

Thank you so much @codeneutrino

I didn’t reach the loop lesson yet, so I was trying to find a way to re-print 3 times (without using the same statement again) given whatever I’ve learned so far up to this exercise.

2 Likes