Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Jacob Charbonneu
Jacob Charbonneu
332 Points

Mathematical help

I am trying to add the items in the list together, but I am not sure where to go from here, can someone help me?

functions.py
# add_list([1, 2, 3]) should return 6
# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
# Note: both functions will only take *one* argument each.
def add_list(list):
  for item in list:

2 Answers

Oktay Altay
Oktay Altay
2,329 Points

you are almost there with cod you have shown above. The only thing left is to add the item in the for loop to a new variable and return the value, like this :

def add_list(list):
    total = 0
    for item in list:
        total += item
    return total

This should give you the sum of the list. I hope this helps :)

[edit: added python formatting -cf]

Max Hirsh
Max Hirsh
16,761 Points

As a more detailed explanation, what you need to due is initialize a variable outside the for loop, then add to it within the for loop for each item. After all that, return the total. Hope this information helps!