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 trialAndrea Campos
4,743 PointsHello there! I'm really stuck with this challenge. Is there any way you could explain it to me? Thank you!
I have no idea what i'm doing.
# 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.
list = (add_list)
def add_list(num):
list.append(num)
print(num+num)
def num():
for num in add_list:
print(num)
3 Answers
Chris Freeman
Treehouse Moderator 68,423 PointsBreaking it down it can be solved using the code below:
def add_list(nums):
'''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.
'''
# Sume the elements of a list using a loop:
result = 0
for num in nums:
result += num
# Return the result
return result
def summarize(nums):
'''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.
'''
# sum the values in list
total = add_list(nums)
# Format result string with original list and total
result = "The sum of {x} is {y}".format(x=nums, y=total)
# returrn result
return result
These can be compressed to:
def add_list(nums):
'''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.
'''
# Using sum() instead of loop to add the elements of a list
return sum(nums)
def summarize(nums):
'''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.
'''
return "The sum of {x} is {y}".format(x=nums, y=add_list(nums))
Talmage Shill
Courses Plus Student 7,593 PointsThe challenge is asking for two functions. The first is going to find the sum of all the numbers in a list. The second is going to show the user what was in the list and what the total is.
def add_list (list):
total = 0
for item in list:
total += item
return total
def summarize (list):
total = add_list(list)
string = str(list)
return "The sum of "+list+" is "+str(total)
Rand Seay
13,975 PointsHello!
The challenge would like you to create two functions: one called add_list
that takes a list as an argument, and the other called summarize
, which also takes a list as an argument. That would look look like this.
# 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(list):
# Add all of the items in the list together here
# The sum starts out as zero
# For each item in the list, add it to the sum
# Return the sum
def summarize(list):
# Return the string "The sum of {list} is {sum}."
# Hint: You need to use the format function and the add_list function you just created
Hope this helps!