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

function

Why is the following code wrong?

def add_list(list_x): sum(list_x)

When I try it in the shell it works fine; however, it appears to be wrong answer in the quiz.

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_x):
  sum(list_x)

3 Answers

Seth Reece
Seth Reece
32,867 Points

You could also do it the long way as the challenge suggests:

def add_list(list):
  i = 0
  for item in list:
    i = i + item
  return i
Seth Reece
Seth Reece
32,867 Points

Hi Sclaudina,

Your function needs to return something when it is called. e.g.

function add_list(list):
    return sum(list)
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I think you mean def instead of function. Been doing JavaScript lately?

I tried as you indicates and also using print() like: def add_list(list_x): print(sum(list_x)) In both cases the answer to the quiz appears to be wrong. However, it works fine offline.

Thanks a lot for your kindness.