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

Can't get code to run on test, but runs everywhere else...please help? Maybe I am not understanding problem?

def add_list(numbers):
  num_list = numbers
  total = sum(num_list)
  print(total)

def summarize(numbers):
  num_list = numbers
  liststring = str(num_list)
  total = sum(num_list)
  totalstring = str(total)
  print("The sum of " + liststring + " is " + totalstring + ".") 


add_list([1, 2, 3])
summarize([1, 2, 3])
Samuel Webb
Samuel Webb
25,370 Points

Provided easier to view formatting for you.

Thank you! :)

2 Answers

Matthew Rigdon
Matthew Rigdon
8,223 Points

In the test, they want you to return answers, not print them. Remember, printing something sends words to your screen, whereas using return is the "answer" from the function. Change both of your last lines to return and that should fix your issue. There is also a little bit of redundancy in your code (unnecessary lines) that you could remove if you want. Here is an example of what I did:

def add_list(items):
  total = 0
  for item in items:
    total += item
  return total

def summarize(lists):
  total = add_list(lists)
  newList = str(lists)
  return("The sum of {} is {}.".format(newList, total))
Samuel Webb
Samuel Webb
25,370 Points

Your summarize method is probably a bit more pythonic than mine. It's been a while since I've used Python.

Thank you, I sincerely appreciate the help :)

Cool...I had to look that one up...LOL you guys are great...appreciate the support.

Samuel Webb
Samuel Webb
25,370 Points

You're over thinking it. For the first one they want you to use a for loop. For the second one all you need is a return value. Here's what I did:

def add_list(l):
  output = 0
  for li in l:
    output += li
  return output

def summarize(l):
  return "The sum of " + str(l) + " is " + str(add_list(l)) + "."

Thank you! :)

Samuel Webb
Samuel Webb
25,370 Points

No problem. Glad to help.