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

Constantin Lungu
Constantin Lungu
8,678 Points

Python Functions

Hello everyone,

Can anyone suggest why I am getting the following error in the attached code?

TypeError: sequence item 0: expected str instance, int found

Thank you, Constantin

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(first_list):
  sum = 0
  for item in first_list:
    sum += item
  return sum

def summarize(second_list):
  y = 0
  for item in second_list:
    y += item
  x = ' '.join(second_list)
  return "The sum of {} is {}.".format(x,y)

3 Answers

Dan Johnson
Dan Johnson
40,532 Points

Your variable second_list contains integers and join requires strings. If you wanted to join them you'd need to convert all the elements into strings.

However you don't have to worry about doing that, the format method knows how to handle a list so you can just pass it in. Also you can avoid repeating the logic for summing a list, just call add_list from summarize.

Mustafa BaลŸaran
Mustafa BaลŸaran
28,046 Points

Hello Constantin,

You can call other functions in a function. Morover, join() concatenates strings to a list by whichever seperator you choose in ''. Alternatively, you may opt for append() instead.

So, the following code would work I assume.

def summarize(second_list):

y = add_list(second_list)
x = []
for item in second_list:
    x.append(item)
return "The sum of {} is {}".format(x,y)

I hope that this is helpful.