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 trialFoday Conteh
279 Pointsfunction.py Challenge task 2 of 2
Need help with challenge task 2 of 2
# 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.
arr = [1, 2, 3]
def add_list(arr):
total = 0
for item in arr:
total += item
return total
def summarize(arr):
list = str(arr)
total = 0
for item in arr:
total = total + item
return " The sum of {} is {}. ".format(sum,total)
2 Answers
rydavim
18,814 PointsYou've got the right idea, just need a couple of changes to pass the challenge.
arr = [1, 2, 3]
def add_list(arr):
total = 0
for item in arr:
total += item
return total
def summarize(arr):
list = str(arr)
total = 0
for item in arr:
total = total + item
# Remove the spaces at the start and end of your string.
# Replace sum with arr, sum and total are the same.
return " The sum of {} is {}. ".format(sum,total)
Frederick Pearce
10,677 PointsYou can also substantially shorten the summarize() function by taking advantage of the first function you created (i.e. add_list()). This is the beauty of functions!
list_1 = [1, 2, 3]
def add_list(list): tot = 0 for item in list: tot += item return tot
def summarize(list): return "The sum of {} is {}.".format(list, add_list(list))