Python Code Challenges: Lists problem "1"

Hello,

I’ve spent the better part of an hour on, “Append Sum”, as follows below.

" 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] ."

After some trial and error I eventually came up with:
"def append_sum(1st):
1st.append(1st[-1] + 1st[-2])
1st.append(1st[-1] + 1st[-2])
1st.append(1st[-1] + 1st[-2])
return 1st

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

Which looks correct. The solution kept giving me a syntax error in line 1. Only after running my code in Atom did I realize that “1st” should be “lst”. The lowercase “L” and number “1” look TERRIBLY similar.

In your experience, what is the best way to deal with this? Did Codecademy do this on purpose to drive this very point home?
I’m honestly very frustrated by this similarity.

Any tips would be much appreciated

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

I may add, that in codecademy, the " 1,l "difference is MUCH harder to spot

We have by this point, one would hope, covered iteration of lists. That involves a loop. When we hear, do something three times, that might by now imply using a loop.

Of course that also involves keeping a bearing on the indices since we will be using variables to represent our data, not fixed literals in a series of repeated lines.

In a perfect world, no errant inputs would ever be given to a function. Programmers bear up to this by writing safeguards; languages do it by regulating data types. Still, GIGO. It comes back to us writing self-protecting functions so that what they return can be trusted.

  if isinstance(lst, list):

and,

    if len(lst) >= 2:

First we need to know that the object has an append attribute, and next we need to know there are at least two elements, else how can we add the last two?

This suggests that we need a method that can suss out the details, and give a valid return, then just loop a given number of times.

>>> def append_sum(lst):
	if isinstance(lst, list):
		if len(lst) >= 2:
			lst.append(sum(lst[-2:]))
	return lst

>>> append_sum([1, 1, 2])
[1, 1, 2, 3]
>>> append_sum([1, 1, 2, 3])
[1, 1, 2, 3, 5]
>>> append_sum([1, 1, 2, 3, 5])
[1, 1, 2, 3, 5, 8]
>>> 

The trick will be to keep that list alive inside a loop, in the form of a variable.

Welcome to the forums!

On my PC (Windows), the text editor is using the Consolas font which is a standard Windows font.

I can see how the two could be confused, but personally I don’t confuse them. (This is probably because of the amount of time I’ve spent professionally staring at PowerShell windows, where I use the Consolas font.)

Other “terminal-esque” fonts have similar… similarities:

image

As do the “raster fonts” in the command line:

image

I don’t think that Codecademy would have done this deliberately to confuse you, but it would seem to be a commonality amongst a number of fonts which look a bit “terminal like” which they could have chosen.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.