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 trialchevano gordon
3,399 Pointsneed help not sure i know what to do
# 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
Cory Madden
7,120 PointsYou 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))
chevano gordon
3,399 Pointschevano gordon
3,399 Pointswhat 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
Treehouse Moderator 68,423 PointsChris Freeman
Treehouse Moderator 68,423 PointsAn "item", sometimes called an "element", is a generic term for what is contained within a
list
or other python container.Cory Madden
7,120 PointsCory Madden
7,120 Pointsitem 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:
The output will be 1, and then on the next iteration it will be 2.
It's similar to writing:
Chris Freeman
Treehouse Moderator 68,423 PointsChris Freeman
Treehouse Moderator 68,423 PointsSometimes "item" is used as a variable name. In this case it represents what ever object is assigned to it.