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 trialAlex Espinosa
3,226 PointsJoin Syntax
---Python def summarize(list): total = add_list(list)
return "The sum of {} is {}".format(''.join(list), total)
I run into problems with this code and believe it relates to the join method. I was thinking potentially that the error I could be receiving is because total is an int, which is being passed into a string but this doesn't seem to be the problem
# 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):
total = 0
for num in list:
total += num
return total
def summarize(list):
total = add_list(list)
return "The sum of {} is {}".format(" ".join(list), total)
1 Answer
Iain Simmons
Treehouse Moderator 32,305 PointsYeah join
concatenates a list of strings together, but you only have integers.
In this case, you don't need the joins, because the desired output representation of the list is the default (square brackets, comma separated).
See here:
>>> [1,2,3].__str__()
'[1, 2, 3]'
>>> [1,2,3].__repr__()
'[1, 2, 3]'
>>> str([1,2,3])
'[1, 2, 3]'
So just past the list itself to format
.