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

Neil Gordon
Neil Gordon
8,823 Points

not sure I am understanding what my error is on this one. My thoughts are I can pass list_of_numbers into my function an

and call that function in return statement.

list_of_numbers = [1,2,3]

def add_list(list_of_numbers):

  for item in list_of_numbers: 
    counter = 3
    counter = counter + item
  return counter

  def summarize(list_of_numbers):

  return ("The sum of" {} "is" {} ".".format(list_of_numbers,counter))  

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

It looks like your def summarize statement is indented too much. Unindent it align with the def add_list statement.

The rest of the code is very close to correct. The format syntax was off and the counter argument was replaced with add_list(list_of_numbers). I've corrected the errors:

def add_list(list_of_numbers):
  counter = 0 # initialize outside of for loop
  for item in list_of_numbers: 
    counter = counter + item
  return counter


def summarize(list_of_numbers):
  return ("The sum of {} is {}.".format(list_of_numbers,add_list(list_of_numbers)))
Neil Gordon
Neil Gordon
8,823 Points

Thank you for the assistance here is my new error TypeError: 'function' object is not iterable

list_of_numbers = [1,2,3]

def add_list(list_of_numbers):
  counter = 0
  for item in list_of_numbers: 
      counter = counter + item
  return counter

def summarize(list_of_numbers):
  return ("The sum of {} is {} .".format(list_of_numbers,add_list(summarize))) 
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

In your code, you have:

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

You are passing the function summarize as an argument in add_list(summarize). The function object can not be iterated upon when it get's to the for loop in add_list().

You need to pass the list_of_numbers from the summarize call as an argument to add_list:

def summarize(list_of_numbers):
  return ("The sum of {} is {}.".format(list_of_numbers,add_list(list_of_numbers))) 
Neil Gordon
Neil Gordon
8,823 Points

ah, my logic was all wrong , will try this

Neil Gordon
Neil Gordon
8,823 Points

Chris Freeman

That worked perfectly thanks for the help!

list_of_numbers = [1,2,3]

def add_list(list_of_numbers):

  for item in list_of_numbers: 
      counter = 3 
      counter = counter + item
  return counter
def summarize(list_of_numbers):
  return ("The sum of {} is {} .".format(list_of_numbers,add_list(list_of_numbers)))