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

Joshua Bivens
Joshua Bivens
8,586 Points

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 have no idea what to do.

Edit: I tried this after checking out James Gill's link:

def summarize(another_list):
  return "The sum of {} is {}.".format(' '.join(another_list), sum(another_list))

and it still didn't work.

Help me.

Joshua,

You're close, but still not there. You're combining format with join, not needed.

def add_list(nkem):

  uchem = 0

  for juju in nkem:

    uchem += juju

 return uchem

def summarize(jude):

  uche = 0

  for ju in jude:

    uche += ju

 return "The sum of {} is {}.".format(jude, uche)

4 Answers

Stone Preston
Stone Preston
42,016 Points

you almost have it. you dont need to use join.

def add_list(another_list):
  return sum(another_list)

def summarize(another_list):
  return "The sum of {} is {}.".format(another_list, sum(another_list))

technically I think they want you to use the add_list function you defined first: instead of using the sum() function

def add_list(another_list):
  return sum(another_list)

def summarize(another_list):
  return "The sum of {} is {}.".format(another_list, add_list(another_list))

I am also having problems with this... I feel like my solution is similar to yours Stone Preston except i don't use sum(). My code is:

# add_list([1, 2, 3]) should return 6
# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."

def add_list(list_of_nums):#takes one parameter which is a list of numbers
  for num in list_of_nums:
    i = 0
    total = list_of_nums[i] + list_of_nums[i +1]
    total = total + total
    i += 1
    return total

  def summarize(list_of_nums):
    return "The sum of {} is {}.".format(str(list_of_nums), add_list(list_of_nums))

when I submit this to be checked I am told that "Bummer! NameError: name 'summarize' is not defined". This doesn't make sense to me. Any suggestions Kenneth Love ?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Hey Xander Taylor you've actually found one of the super-useful but hard-to-understand areas of Python, which is that functions can be defined inside of functions. Your summarize function is indented inside of your add_list function, so it can't be found by the test runner. Pull it out and you shouldn't have that error any more.

Ruby R
Ruby R
1,086 Points

def add_list(addlist): list_tot=0 for i in addlist: list_tot= addlist[0]+addlist[1]+addlist[2] return list_tot def summarize(s_list): return "The sum of {} is {}.".format(addlist,list_tot)