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

Basic Python Help

QUESTION: Now, make a function named summarize that also takes a list. It should return the string "The sum of X is Y.", replacing "X" with the string version of the list and "Y" with the sum total of the list.

I'm not sure where to go thus far.

MY CODE:

    # add_list([1, 2, 3]) should return 6
    # summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
    # Note: both functions will only take *one* argument each.

def add_list(nums):
  return sum(nums)

if summarize():
  return string("The sum of {} is {}".format(something,something)

1 Answer

Emil Rais
Emil Rais
26,875 Points

You create a function using the def keyword, you seem to have used the if keyword instead. Your function should accept a parameter (you name it in your parenthesis). It is that parameter which you pass to your format function and also that parameter which you should sum.

Can you take it from there, Diego?

Whoops. Yes, got it. Thanks so much Emil Rais

Actually thought I had it but response said "Oops it looks like task 1 is no longer passing." I did not even change it. Take a look please. Emil Rais

def add_list(nums):
  return sum(nums)

def summarize(sum):
  return ("The sum of {} is {}".format(add_list, sum(add_list))
Emil Rais
Emil Rais
26,875 Points

If you name the parameter to summarize differently I think it will be easier for you. Summarize is not supposed to receive a sum, but a list of numbers. Try renaming the parameter to be called numbers and then try and see if you can figure it out. Hint: One of the arguments to format should make use of add_list.