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 trial

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

Tomomi Arita
Tomomi Arita
601 Points

TypeError: 'str' object cannot be interpreted as an integer

I am stuck with setting up summarize function. I cannot convert the int list [1, 2, 3] to string display 1, 2, 3.

I am keep getting the error. TypeError: 'str' object cannot be interpreted as an integer

functions.py
# add_list([1, 2, 3]) should return 6
def add_list(number_list):
  total = 0
  for number in number_list:
    total = int(total) + int(number)
  return total

add_list([1, 2, 3])

# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
def summarize(numbers):
  sum_total = add_list(numbers)

  sum_string = ', '.join(str(x) for x in numbers)
  new_string = "The sum of " + bin(sum_string) + " is " + bin(sum_total) + "."

  return new_string


# Note: both functions will only take *one* argument each.

2 Answers

Joshua Edwards
Joshua Edwards
52,175 Points

you can directly use str(array_variable) to convert the array to a string and the same for the sum_total. so you get something like return "The sum of " + str(numbers) + " is " + str(sum_total)

Tomomi Arita
Tomomi Arita
601 Points

Thank you, Joshua. It worked!

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You definitely don't need the bin() function since it gives you the binary representation of an integer.