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

ERIC MILTON
ERIC MILTON
1,170 Points

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

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 string version of the list and "Y" with the sum total of the list

Below is my code please help

functions.py
list= [1, 2, 3]
list= [1, 2, 3]
def summarize(list):
  total = 0
  for item in list:
    total += item

  return total

print("The sum of"+' '+str(list)+' '+'is'+' '+str(summarize(list))+".")

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

[edit formatting --cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

You have all the pieces they just need to be rearranged. It looks like you renamed the add_list() function to summarize()

Change the name back to add_list(), then create an addition function named summarize() that calls add_list and uses the result to populate the string from your print statement. Lastly, summarize should return the formatted string instead of printing it.

andrew pande
andrew pande
1,905 Points

Hi thanks for the answer but how do you call the add_list function or better yet if you have a code please let me have it I have been stuck on this for days now thanks once again

Shazad Kazi
Shazad Kazi
10,758 Points
def add_list(something):
  result = 0
  for item in something:
    result += item
  return result
def summarize(something):
  return "The sum of " + str(something) + " is " + str(add_list(something)) + "."