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

redux quiz

Make a function named add_list that takes a list. The function should then add all of the items in the list together and return the total. Assume the list contains only numbers. You'll probably want to use a for loop. You will not need to use input()

i cant figure out how to code this help please!

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.

3 Answers

Karthik Mohandas Bangera
Karthik Mohandas Bangera
2,519 Points
def add_list(number_list):
    sum = 0
    for item in number_list:
        sum = sum + item
    return sum

def summarize(number_list):
    sum = 0
    for item in number_list:
        sum = sum + item
    return "The sum of {} is {}".format(number_list,sum)

number_list = [1,2,3]
sum = add_list(number_list)
result = summarize(number_list)
print sum
print result

thank you a lot. i just couldnt get some of the syntax down right tanks a ton

Nathan McElwain
Nathan McElwain
4,575 Points

Solution without the for loop:

def add_list(x):
    y = sum(x)
    return y

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

cool you just showed me how to do a double .format input too.thanks