Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Functions

Pablo Esteban Aguilar Castro
Pablo Esteban Aguilar Castro
1,141 Points

TypeError: Can't convert int object into string object implictly

I'm using str() to the list, but it returns me this error.

functions.py
# 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
MOD
Chris Freeman
Treehouse Moderator 68,064 Points

The 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())