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

Could someone help with the Functions code challenge?

I am completely baffled by this code challenge. I don't know where to start. I barely understand how the for loops work, and I have forgotten how to add the values together. Could anyone give me advice on how to complete this code challenge?

Here's my answer:

def add_list(x):
  total = 0
  for item in x:
    total = total + item

  return total

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

1 Answer

Here's the answer, please ask if you didn't understand something:

# 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(my_list):
    total_sum = 0
    size = len(my_list)
    for number in range(size):
        total_sum += my_list[number]

    return total_sum


def summarize(other_list):
    total_sum = add_list(other_list)   #here we may use the function defined before
    my_string = "The sum of {} is {}".format(other_list,total_sum)

    return my_string