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 trialERIC MILTON
1,170 PointsNow, 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
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
Treehouse Moderator 68,441 PointsYou 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.
Shazad Kazi
10,758 Pointsdef 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)) + "."
ERIC MILTON
1,170 PointsThank you !
ERIC MILTON
1,170 PointsERIC MILTON
1,170 PointsThank you !
andrew pande
1,905 Pointsandrew pande
1,905 PointsHi 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
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsA complete answer with code can be found in this answer