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

Why is this not working? it says I am not returning a string total=0 for x in the_list:

def add_list(the_list): total=0 for x in the_list: total=total+x

return total

def summarize(a_list): add_list(a_list) string_list=[] for number in a_list: string_list.append = str(number)

complete = "The sum of {} is {}.".format(string_list, total) return complete

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(the_list):
  total=0
  for x in the_list:
    total=total+x

  return total

def summarize(a_list):
  add_list(a_list)
  string_list=[]
  for number in a_list:
    string_list.append = str(number)

  complete = "The sum of {} is {}.".format(string_list, total)
  return complete

2 Answers

You're using total to format your string, but you have no access to it. When you call add_list() in your summarize() function you're not assigning the value to any variables. Also, looping through the function creating a new list of strings is not what they want, but instead the original list.

Hey Brenda!

Just to expand on and clarify what Cory has said: Variable names only exist for the duration of the function that they live in. So, when you are referencing total in task 2 of the challenge, Python has no idea what that is because you haven't defined any variable named total within summarize() and you can't access the variable total within add_list() (at least, not in the way you're trying to do so here.)

What you can do, however, is create a new variable (also named total if you want, or anything else) within summarize() and then send the list that is passed into summarize() (which you've named a_list) over to add_list() and assign it to total. In other words, something like this: total = add_list(a_list) Or, you could just skip the variable assignment altogether and call the add_list(a_list) function from within your .format() call.

Another thing to note is that this part:

  string_list=[]
  for number in a_list:
    string_list.append = str(number)

is totally unnecessary. The list (which you've named a_list in this case) is already being passed in as a list of numbers; there is no need to reassign all of the list elements to a new list. You can just use a_list directly, again by sticking it directly into the .format() call, if you wanted.

I hope this helps! Please mark this as the Best Answer if it was most helpful.

Thanks! and Be Well, Graham