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

Robert Aguilera
Robert Aguilera
1,222 Points

I keep getting the error "Can't convert list object to str implicitly. Any help?

What am I missing? The instructions are to:

Now, make a function named summarize that also takes a list. It should return the string "The sum of X is Y.", replacing "X" with the string version of the list and "Y" with the sum total of the list.

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.

shopping_items = []

def add_list(item):
  shopping_items.append(item)
  return sum(item)

def summarize(str):
  return ' '.join("The sum of " + shopping_items + "is " + add_list)

3 Answers

Mikael Enarsson
Mikael Enarsson
7,056 Points

I'm not sure what you went for here.

Going to the challenge, it seems that the first task is actually to add the numbers provided in a list as an argument, whereas your function adds the "item" to "shopping_list". I would solve the problem like this:

def add_list(a_list):
  sum = 0
  for num in a_list:
    sum += num
  return sum

Now, for the second problem, since they want you to print the argument list, you need to have a variable name in the method signature, like "a_list", but right now you have "str", which is reserved. Also, you are going in the right direction in trying to use the "add_list" function, but when you call a function you have to have parentheses enclosing the function argument, which in this case would be "a_list":

def summarize(a_list):
  return "The sum of {} is {}".format(a_list, add_list(a_list))

Also, note that I'm using the ".format()" method, as this seems to fix the "Can't convert list object to str implicitly." problem.

You could also do it using the ".join()" method, like so:

def summarize(a_list):
  return "The sum of [" + ", ".join(a_list) + "] is " + str(add_list(a_list))

though this is quite cumbersome and the first way really is to prefer.

If you have any questions, just ask, and I'll get back to you as soon as possible.

Also, if you found this helpful, a "Best answer" vote would be great ^^

To help you out, we'll break down what your current code is doing.

Let's start with the the first half of your answer. Your add_list function takes in an "item." This specific problem gives you a list of items (ex. [1,2,3]) instead of just one item. So when you append the "item," you're actually trying to append the whole list to the "shopping_items" list. So running your add_list function returns the sum of all the items within the given list ([1,2,3]) but it also appends that list within another list, shopping_items.

shopping_items = []

def add_list(item):
  shopping_items.append(item)
  return sum(item)

Now this next part is where you're getting the error. That error just means that python can not automatically convert the values that you want to use into a string. Right now the summarize function is trying to "join" one string. It's just one string because "The sum of" + shopping_items + "is" + add_list is attempting to use string concatenation but remember that shopping_items is a list of one list from before. So what you're actually trying to string together is "The sum of " + [[1, 2, 3]] + is " sum(item). The error comes from both the list you're trying to add but also the number value at the end. You can't concatenate two different values (str and int). If you specifically change the last item to str(add_list), that would change the int value into a str allowing you to concatenate the strings. Now the ''.join is usually used for lists in combining the items of a list but here you're trying to combine one long sentence so there may not be a reason to use that at all. If you had a list that contained ["The sum of", str(shopping_items), "is", str(add_list)] then the join method would be used. Also the problem is giving you an argument to use within the function.

def summarize(given_list):
  return ("The sum of " + str(given_list) + "is " + str(add_list))
# I tried to improve what you had by using the given argument and changing that last value

Hope this helps. =]

Robert Aguilera
Robert Aguilera
1,222 Points

Hey thanks to you both, cos with both your examples I was able to get on the right path. I really appreciate the time you guys took to answer my question. It's very much appreciated. This was how I got it to work.

shopping_list = []

def add_list(item):
  shopping_list.append(item)
  return sum(item)

def summarize(a_list):
  return "The sum of {} is {}.".format(a_list, add_list(a_list))```