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

Foday Conteh
Foday Conteh
279 Points

function.py Challenge task 2 of 2

Need help with challenge task 2 of 2

functions.py
# 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.
arr = [1, 2, 3]
def add_list(arr):
  total = 0
  for item in arr:
    total += item
  return total

def summarize(arr):
  list = str(arr)
  total = 0
  for item in arr:
    total = total + item
  return " The sum of {} is {}. ".format(sum,total)

2 Answers

rydavim
rydavim
18,814 Points

You've got the right idea, just need a couple of changes to pass the challenge.

arr = [1, 2, 3]
def add_list(arr):
  total = 0
  for item in arr:
    total += item
  return total

def summarize(arr):
  list = str(arr)
  total = 0
  for item in arr:
    total = total + item

  # Remove the spaces at the start and end of your string.
  # Replace sum with arr, sum and total are the same.
  return " The sum of {} is {}. ".format(sum,total) 
Frederick Pearce
Frederick Pearce
10,677 Points

You can also substantially shorten the summarize() function by taking advantage of the first function you created (i.e. add_list()). This is the beauty of functions!

list_1 = [1, 2, 3]

def add_list(list): tot = 0 for item in list: tot += item return tot

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