Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Functions

Andrea Campos
Andrea Campos
4,743 Points

Hello 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.

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.
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
MOD
Chris Freeman
Treehouse Moderator 68,064 Points

Breaking 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
PLUS
Talmage Shill
Courses Plus Student 7,593 Points

The 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)

Hello!

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!