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 trialDavid Monroe
1,505 PointsChallenge Task 2 of 2 Python Basics. Functions.py
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.
#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.
def add_list(some_list): sum = 0 for number in some_list: sum = number + sum return sum
def summarize(another_list): sum = 0 for number in another_list: sum = number + sum return "The sum of {} is {}".format(str(another_list), sum)
I AM GETTING THE ERROR MESSAGE…
Bummer! summarize returned the wrong output. Got 'The sum of [1, 2, 3] is 6' instead of 'The sum of [1, 2, 3] is 6.’.
I’ve tried to output different variable types thinking maybe I output a string when an int or whatever was needed…but nope. can someone tell me what I’m doing wrong?
# 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.
def add_list(some_list):
sum = 0
for number in some_list:
sum = number + sum
return sum
def summarize(another_list):
sum = 0
for number in another_list:
sum = number + sum
return "The sum of {} is {}".format(str(another_list), sum)
5 Answers
Kenneth Love
Treehouse Guest TeacherI've fixed the validation so it's more forgiving.
Mikael Enarsson
7,056 PointsNo problem! One thing I've noticed is that the more mistakes you make, the easier they are to spot when you make an error XD
Mikael Enarsson
7,056 PointsThis code is totally right, with one stupid little error that you really should pass with. I'll tell you now.
Ready?
Type a dot ('.') at the end of the string (after the "{}" but before the '"'), like so:
def summarize(another_list):
sum = 0
for number in another_list:
sum = number + sum
return "The sum of {} is {}.".format(str(another_list), sum) #Added the dot
The code challenges are really inflexible ><
David Monroe
1,505 PointsWOW! ha. Thanks so much. Can't tell you how many times I've gone over it. Very much appreciated.
deronholmes
7,389 PointsSo the way I've thought of it was to use the exact same list, calling the previous function in the return statement of the second.