Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,064 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,064 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)))