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 trialAddie Prinz
522 PointsI am stuck on this challenge! I just don't get it. help!
I can't figure it out. I reviewed a lot too. Help, please?
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsPutting the Fun Back in Functions Challenge Part 1 of 2:
Make a function named add_list that takes a list. The function should then add all of the items in the list together and return the total. Assume the list contains only numbers.
Let's take it step-by-step.
Make a function named add_list that takes a list
The instructions might be said as "Make a function named add_list that takes a single argument. The function can expect the argument to be a list. The function below takes a single argument.
def add_list(list_in):
# this function does nothing but return
return
The function should then add all of the items in the list together.... Return total
def add_list(list_in):
# initialize result to zero
result = 0
# for each item in list_in
for value in list_in:
# add item to result
result = result + value
# return total
return result
The statement result = result + value
can be abbreviated as result += value
.
Challenge Task 2 of 2 :
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.
def summarize(list_in):
# Set X to input list
X = list_in
# Set Y to sum of items in list using add_list() function
Y = add_list(list_in)
# return string
return "The sum of {} is {}".format(X, Y)
This can be compress into a single statement:
def summarize(list_in):
return "The sum of {} is {}".format(list_in, add_list(list_in))
Robert Richey
Courses Plus Student 16,352 PointsThat's a remarkable answer, thanks for this.
Adam Van Antwerp
3,104 PointsWhat an awesome answer! Rock on!
Ryan Duchene
Courses Plus Student 46,022 PointsGreat answer, +1 for you! :)
Robert Richey
Courses Plus Student 16,352 PointsRobert Richey
Courses Plus Student 16,352 PointsHi Addie,
Welcome to Treehouse!
None of your code is showing. Can I have you copy/paste the code you've tried and I'll do my best to help you through this.
Kind Regards