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

Code challenge 1 of 2 error

I have this code, which should be correct and I looked at some other people who I asked the same question and matched one with theirs, but it still gives me an error

def add_list(items):
  total = 0
  for num in items:
    total += num
  return total

.

You're very close. Take a look at your loop, you're saying for each num in the items array (that part is good). Then you're saying to add the value of items to the existing variable called total, think about that part there.

Looks like you self corrected the original post. What you have up there now works for me.

Interesting. It still gives me an error.

your original code was:

def add_list(items):
  total = 0
  for num in items:
    total += items
  return total

You now have:

def add_list(items):
  total = 0
  for num in items:
    total += num
  return total

You're trying the second one, right?

Yes, I am. I initially had that code and was retyping in when making this thread and accidentally put in items. I have been using the second one the whole time.

I got. I removed the comment add_list which is why I got the error.

You might want to go ahead and summarize what you did that made it work in an answer post so you can close the question out.

1 Answer

Well my mistake was that I had this code that was commented out included in my challenge. I thought I need it in order to make the function and make it work.

add_list([1, 2, 3]) should return 6

Once I commented it back out

# add_list([1, 2, 3]) should return 6

The code worked.