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 trialBach Vu
8,293 Pointsdef summarize(list_num): sum = add_list(list_num) print("The sum of {} is {}.".format(list_num, sum))
Why I got this code wrong? I tested and it really worked but the challenge task gave me the bummer.
# 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(list_num):
total = 0
for num in list_num:
total += num
return total
def summarize(list_num):
sum = add_list(list_num)
print("The sum of {} is {}.".format(list_num, sum))
2 Answers
Stone Preston
42,016 Pointstask 2 states: 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.
you need to return the string, not print it:
# 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(list_num):
total = 0
for num in list_num:
total += num
return total
def summarize(list_num):
sum = add_list(list_num)
return "The sum of {} is {}.".format(list_num, sum)
Ron Fisher
5,752 PointsI believe the output to the screen is correct and what they are looking for. However, in the background you have something else going on. Try this in the python shell:
type(list_num)
type(str(list_num))
You will see that one is a of the Class 'list' and one of the Class 'str'. I believe the challenge was to make sure you output to the screen the string representation not the list itself.
I hope this helps.
Bill Talkington
11,840 PointsBill Talkington
11,840 PointsI had the same issue; kept trying to print it. Thanks!