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

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,773 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!