Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Functions

Wrong exercise dynamic in Python lists

So it says it passes 3 elements but it wants to have 6 as an answer. the only way I see this working out was like this

shopping_list = [] 

def add_list(items):
  for i in items: 
    shopping_list.append(i)
    shopping_list.append(i)

  return len(shopping_list)

Notice how I had to duplicate the ** append() **. It makes me thing the exercise if wrongly constructed.

4 Answers

Peter Szerzo
Peter Szerzo
22,661 Points

It's a bit more involved than that. I'll give away part of the answer:

def add_list(items):
  sum = 0
  for item in items:
    # magic goes here
  return sum
Peter Szerzo
Peter Szerzo
22,661 Points

Ani,

The expected number 6 is the sum of the three numbers, not the length of the list. Of course, if you force the length of the list to be 6 by adding every number in there twice and return this length, then you may pass the challenge. But this is pure coincidence.

Try implementing the sum of the list items by setting a sum variable to 0 at the beginning of the function, and adding each list item to it within to for loop (the one you already have).

Peter

sum(items)

?

...
    sum += item
...