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 trialY. Kravets
10,350 PointsQuestion 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:
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?
Unless I am misunderstanding the task isn't that the case that add_list and summarized are required to do the same thing here?
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
14,335 PointsHello 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
13,023 PointsI 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
10,350 PointsThanks 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
14,335 PointsSure 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):
Kenneth Love
Treehouse Guest TeacherPython functions don't have explicit returns, so you have to do the return
yourself.