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

need help not sure i know what to do

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.
my_list = []

def add_list(items):
 my_list.append(item)
total = 0
total+=my_list[]
return total

[edited format --cf]

1 Answer

You need to write two different functions. One that adds all the list items and returns the sum, and another that uses that function and returns a string.

def add_list(some_list):
  sum = 0
  for item in some_list:
    sum += item;
  return sum

And your second function will be:

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

what is item. is it a key word or is it define somewhere else in the code and if it is what is its purpose in the code. thank you for the help

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

An "item", sometimes called an "element", is a generic term for what is contained within a list or other python container.

item is just a variable we create on the spot in a for loop.The word "item" in this case could be anything. So on each loop it increments the index of the value it holds. On the first loop in:

for item in [1, 2, 3]:
  print(item)

The output will be 1, and then on the next iteration it will be 2.

It's similar to writing:

some_list = [1, 2, 3]
i = 0
while i < len(some_list) - 1:
  print(some_list[i])
  i += 1
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Sometimes "item" is used as a variable name. In this case it represents what ever object is assigned to it.