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

Make a function named add_list that takes a list. The function should then add all of the items in the list together and

i need help

functions.py
def add_list(lst):
  result = 6
  for item in lst:
    result += item 
    return result

8 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi alec burnett , I noticed that you've previously posted about this question at least 4 times.

Imgur

And many people have provided answer for in your previous posts, I'd strongly suggest you go check them out instead of continue making new posts. Thanks.

i did non of them are working thats why i keep asking

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

OK, let's try to solve this problem once and for all here. :)

So in your codes, there're couple of places need changes.

  1. the initial value of result should be 0 instead of 6
  2. your indentation of return result isn't right, it should be indented outside of the for loop, that way you can return result 's value after the for loop is finished running.
def add_list(lst):
  result = 0
  for item in lst:
    result += item 
  return result

i just tried that and it didn't work

ok thank you William lee that worked now my friend told me the second part but it didn't work can you help me there as well

William Li
William Li
Courses Plus Student 26,868 Points

Yes, of course, can you show me the code you've written for part 2?

how do i show the code sorry im new to this treehouse thing lol

William Li
William Li
Courses Plus Student 26,868 Points

just copy the code over and post it here, if the format is off, I'll fix that for you.

def add_list(lst) :
  result = 0 
  for item in lst:
    result += item
  return result 
def summarize (another_list) :
  return "The sum of {} is.".format(' '.join(another_list), sum(another_list))

here you go william lee

William Li
William Li
Courses Plus Student 26,868 Points

ok, got your code here, give me a second to fix it for you.

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hello, alec burnett

You got alot of it correct in your code :P. There're some places need changes. First, take another look of what the challenge is asking you to do

It should return the string "The sum of X is Y.", replacing "X" with the string version of the list and "Y" with the sum total of the list.

summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."

So in the return String, there're two places you need to insert values into, therefore, you need 2 {} inside your return String, not just 1.

  • the first {}, should be just the argument another_list.
  • the second {}, should be the sum of the list, which you got it correct, sum(another_list) is the way to go.

And the final code should be.

def add_list(lst):
  result = 0
  for item in lst:
    result += item 
  return result

def summarize (another_list) :
  return "The sum of {} is {}.".format(another_list, sum(another_list))

Hope you find it useful, if you have more question, feel free to post it here.

Mathieu HAYS
Mathieu HAYS
9,623 Points

I haven't check what is asked but I can tell that your return is not placed at the right spot. Currently this return will end your function after one iteration of the loop. That's not what you want here.

def add_list(lst):
    result = 6
    for item in lst:
        result += item 

    return result