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

Y. Kravets
Y. Kravets
10,350 Points

Question about making list a function argument

I am trying to solve this final task for this block and either the solution is very simple or I am simply failing to comprehend the task. My question is the following:

  1. The list to be summed over will be provided as an argument of the function once we call it in the main program. So how do we define the list to be our argument in 'def' so we can actually sum over its elements?

  2. Unless I am misunderstanding the task isn't that the case that add_list and summarized are required to do the same thing here?

functions.py
def add_list(my_list):
  sum = 0
  for item in my_list:
    sum = sum + item

add_list([1,2,3])

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

3 Answers

Juan Martin
Juan Martin
14,335 Points

Hello my friend, you're doing a great job, there's a little thing you forgot to put in your add_list function and that's the "return" statement.

For the last part, the "summarize" function is different from the add_list function, you may think they do the same but the "summarize" function all that does is to return the result as the code challenge states, and you get that result by using the add_list function inside of it. This is how it'll look like at the end:

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

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

Hope this helps :)

Ricky Catron
Ricky Catron
13,023 Points

I think I understand what your asking correct me if I'm wrong.

"So how do we define the list to be our argument in 'def' so we can actually sum over its elements?"

You would pass it in as a parameter just like your code shows

def add_list(my_list): #my_list equals whatever goes in the parentheses () after add_list
  sum = 0
  for item in my_list:
    sum = sum + item

add_list([1,2,3]) #my_list equals [1,2,3] here

Your second question

"Unless I am misunderstanding the task isn't that the case that add_list and summarized are required to do the same thing here?"

This is almost correct but look at the comment which tells you what they should return.

add_list should just return the sum as an int.

summarize should return a string with the list and sum filled in.

"The sum of MY_LIST_HERE is SUM_HERE."

They look similar on the surface but have very different purpases.

A hint, you could call add_list from within summarize to get the sum without repeating code

Lastly you need to add a return statment at the end of add_list so it returns sum.

Goodluck! --Ricky

Y. Kravets
Y. Kravets
10,350 Points

Thanks you guys for a rapid response, its been bugging me for a few hours. Seems like I have actually been close. Any chance you can provide simple down to earth explanation of 'return' in python and why does it have to be used with the function? I would have thought that since we are self-updating the 'sum' variable within the loop it will store the value at the end of the day and reveal it once the function is called for a given list?

Although after careful look at your comments: does return is sort of saying print (output) this particular variable once the function is called?

Juan Martin
Juan Martin
14,335 Points

Sure my friend, the return statement ends the function and gives that value as an output and can be stored in a new variable when you call the function (if needed). You can check these 2 links here that explains that very well (with examples):

http://www.tutorialspoint.com/python/python_functions.htm

http://learnpythonthehardway.org/book/ex21.html

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Python functions don't have explicit returns, so you have to do the return yourself.