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

Putting the "Fun" Back in "Function" Challenge Task 2 of 2

I'm getting the following error: summarize returned the wrong output. Got 'None' instead of 'The sum of [1, 2, 3] is 6.'.

I'm sure it's something silly. Any guidance as to what I'm doing wrong would be greatly appreciated.

Here is my code:

def add_list(nums):
  total = sum(nums)
  return total

def summarize(values):
  msg = str(values)
  total = add_list(values)
  print("The sum of {} is {}.".format(msg,total))

6 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

return will return any Python value. String, list, number, custom class, another function...anything.

Hi Imer,

The challenge wants you to return the value rather than print it.

Hi Jason, Thank you for answering. I'm still a little confused, though. I thought return would only return mathematical values, not strings. Is that not a true statement? For the summarize function they are asking for a string. Can I return a string? The code below is my latest attempt:

def add_list(nums):
    total = sum(nums)
    return total

def summarize(nums):
    msg = str(nums)
    total = add_list(nums)
    summary = "The sum of {} is {}.".format(msg,total)
    return summary

Thanks Kenneth! I finally realized the error of my ways after reading your response. Turns out I was forgetting to convert the argument's total to a string. So my big take-away from this is the .format() method does not work with integers.

Here is my final (passing!) code:

def add_list(nums):
     total = sum(nums)
     return total

def summarize(myList):
     msg = str(myList)
     total = str(sum(myList))
     return "The sum of {} is {}.".format(msg,total)
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

.format() will work just fine with non-strings. If you were getting errors using it that way before, see if you can reproduce them. I'd love to see what was erroring and how.

Oh OK. Will do. Thanks again!

Well just you said, Kenneth, it worked like a charm with a non-string. Unfortunately I did keep versions of all my code samples so I cannot reproduce any errors. But the good news is I'm learning some good stuff here!

Here's my latest attempt with a non-string:

def add_list(nums): total = sum(nums) return total

def summarize(myList): msg = str(myList) total = sum(myList) return "The sum of {} is {}.".format(msg,total)

Thanks again Kenneth!