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

Now, make a function named summarize that also takes a list. It should return the string "The sum of X is Y.", replacing

im really confused

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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Task: **Now, make a function named summarize that also takes a list. It should return the string "The sum of X is Y.", replacing X with the list and Y with the list sum.

Your add_list() needs two corrections:

def add_list(lst) :
    result = 0 # <-- initialize to zero
    for item in lst:
        result += item
    return result # <-- outdent outside for for loop

You already have the ability to add a list in your add_list() function. Let's use it in the summarize() function.

def summarize(list):
    X = list
    Y = add_list(list)
    result = "The sum of {} is {}".format(X, Y)
    return result

Or, combining all this lines, it can be compressed to:

def summarize(list):
    return "The sum of {} is {}".format(list, add_list(list))
Stephen Bone
Stephen Bone
12,359 Points

Hi Chris

Good answer, you've been beating me to the python queries all night :)

Just thought I'd mention that add_items(list) should be add_list(list).

Stephen

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Stephen Bone: Thanks for the feedback. I've updated my answer. It's tough keeping ahead of you! Actually, I just have been lucky refreshing in a timely fashion.

andrew pande
andrew pande
1,905 Points

Hi thanks for the answer chris freeman you have just saved from a all lot of frustration!