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 trialPablo Esteban Aguilar Castro
1,141 PointsTypeError: Can't convert int object into string object implictly
I'm using str() to the list, but it returns me this error.
# 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(my_list):
i = 0
for item in my_list:
i += item
return i
def summarize(my_list):
i = 0
for item in my_list:
i += item
return "The sum of {} is ".format(my_list) + i
1 Answer
Chris Freeman
Treehouse Moderator 68,423 PointsThe issue is with your last line:
return "The sum of {} is ".format(my_list) + i
"The sum of {} is ".format(my_list)
creates a string, while i
is an integer. To fix, wrap in a str()
:
return "The sum of {} is ".format(my_list) + str(i)
Actually, the challenge is asking you to utilize the add_list()
function:
return "The sum of {} is ".format(my_list) + str(add_list())
But you can combine these in the format
, which will cast the integer sum as a string for you:
return "The sum of {} is {}".format(my_list, add_list())