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 trialTrevor Devalle
Courses Plus Student 992 PointsPython - typeError: Can't convert 'int' object to str implicitly
I really can't understand what is wrong with my code. I also tried it on my computer and everything works!
I will appreciate any kind of help.
Thank you! This error is drive me crazy!
# 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(intlist):
return sum(intlist)
def summarize(intlist):
strintlist = ''.join(str(e) for e in intlist)
return 'The sum ' + strintlist + ' is ' + sum(intlist) + "."
1 Answer
Charles Lee
17,825 Pointsdef summarize(intlist):
strintlist = ''.join(str(e) for e in intlist)
return 'The sum ' + strintlist + ' is ' + sum(intlist) + "."
The problem here is that in the last line of this method, we're trying to concat a str and an integer together. Python, by default, will not automatically convert that for you.
def summarize(intlist):
strintlist = ''.join(str(e) for e in intlist)
return 'The sum ' + strintlist + ' is ' + str(sum(intlist)) + "."
A better way for string interpolation is to use the .fortmat
method.
def summarize(intlist):
strintlist = ''.join(str(e) for e in intlist)
return 'The sum {} is {}.".format(strintlist, sum(intlist))
Hope this helps! :]