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

Kody Dibble
Kody Dibble
2,554 Points

Python list sum help

I can't seem to figure out the code to add these list items together. Thanks..

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.
def add_list([1, 2, 3])
return sum(add_list)

2 Answers

Hi Kody Dibble,

you have to iterate over the list with a loop and add the current value of the loop iteration to a variable which holds the sum. Also look again at the second comment line. Your string output should look like what is stated and not just return the sum. Last but not least you to have to stick to the Python indentation rules. The body of your function is not correctly indented. Currently your return statement is outside of your add_list function.

Maybe you should rewatch the video so you get a better grasp.

I don't want to just post the answer because I think you will learn much better if you only get tips and solve those things yourself. It is pretty good explained in the videos.

# 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([1, 2, 3]):   # notice the colon at the end of this line
   # create a variable which will hold the sum
   # create a loop and add the current value to the variable
   # return something like this "The sum of [1, 2, 3] is 6."

Please don't hesitate to asked some more if you still don't know what may be wrong.

Kody Dibble
Kody Dibble
2,554 Points

Thanks, I was confused on how to add the list and return the sum, still unsure going to rewatch the video Thanks a lot!

You are welcome Kody.

I think it really would be better if you rewatch some videos because those things are the fundamentals and if you just get the answer and go further you will only have more problems because you may didn't understand what is wrong and why.

If you understand what Kenneth tells you in the videos, this exercise will be quite easy for you I think. But still you should ask if you still have problems :-)