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 trialImer Pacheco
9,732 PointsPutting 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
Treehouse Guest Teacherreturn
will return any Python value. String, list, number, custom class, another function...anything.
Jason Anello
Courses Plus Student 94,610 PointsHi Imer,
The challenge wants you to return
the value rather than print it.
Imer Pacheco
9,732 PointsHi 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
Imer Pacheco
9,732 PointsThanks 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
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.
Imer Pacheco
9,732 PointsOh OK. Will do. Thanks again!
Imer Pacheco
9,732 PointsWell 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!