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 trialJoshua Bivens
8,586 PointsNow, 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.
I have no idea what to do.
Edit: I tried this after checking out James Gill's link:
def summarize(another_list):
return "The sum of {} is {}.".format(' '.join(another_list), sum(another_list))
and it still didn't work.
Help me.
applehead
5,861 Pointsdef add_list(nkem):
uchem = 0
for juju in nkem:
uchem += juju
return uchem
def summarize(jude):
uche = 0
for ju in jude:
uche += ju
return "The sum of {} is {}.".format(jude, uche)
4 Answers
James Gill
Courses Plus Student 34,936 PointsJoshua,
See my answer here, maybe it will help. https://teamtreehouse.com/forum/how-do-i-make-a-funcwtion-addlist-to-return-the-numbers-added-in-a-list-i-used-sum-but-cannot-write-in-a-function
Stone Preston
42,016 Pointsyou almost have it. you dont need to use join.
def add_list(another_list):
return sum(another_list)
def summarize(another_list):
return "The sum of {} is {}.".format(another_list, sum(another_list))
technically I think they want you to use the add_list function you defined first: instead of using the sum() function
def add_list(another_list):
return sum(another_list)
def summarize(another_list):
return "The sum of {} is {}.".format(another_list, add_list(another_list))
Xander Taylor
12,390 PointsI am also having problems with this... I feel like my solution is similar to yours Stone Preston except i don't use sum(). My code is:
# add_list([1, 2, 3]) should return 6
# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
def add_list(list_of_nums):#takes one parameter which is a list of numbers
for num in list_of_nums:
i = 0
total = list_of_nums[i] + list_of_nums[i +1]
total = total + total
i += 1
return total
def summarize(list_of_nums):
return "The sum of {} is {}.".format(str(list_of_nums), add_list(list_of_nums))
when I submit this to be checked I am told that "Bummer! NameError: name 'summarize' is not defined". This doesn't make sense to me. Any suggestions Kenneth Love ?
Kenneth Love
Treehouse Guest TeacherHey Xander Taylor you've actually found one of the super-useful but hard-to-understand areas of Python, which is that functions can be defined inside of functions. Your summarize
function is indented inside of your add_list
function, so it can't be found by the test runner. Pull it out and you shouldn't have that error any more.
Ruby R
1,086 Pointsdef add_list(addlist): list_tot=0 for i in addlist: list_tot= addlist[0]+addlist[1]+addlist[2] return list_tot def summarize(s_list): return "The sum of {} is {}.".format(addlist,list_tot)
James Gill
Courses Plus Student 34,936 PointsJames Gill
Courses Plus Student 34,936 PointsJoshua,
You're close, but still not there. You're combining format with join, not needed.