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

Function Error

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. Assume the list contains only numbers. You'll probably want to use a for loop. You will not need to use input().

I can't understand what wrong with my code:

def add_list([1,2,3])
   for item in add_list:
       print (item+item+item)

[MOD: added ```python markdown formattting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 67,989 Points

Sorry, your code didn't post to the forum. Can you please add it to your post?

Now can you see it ?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

There are 6 issues with your code:

def add_list(a_list):  # <--#1 missing colon at end of line. #2 provide parameter instead of hardcoded value
    total = 0  # <-- need to initiate total before loop
    for item in a_list:  # <-- operate on parameter instead of function name
        total = total + item # <-- need to accumulate total as for loop runs
    return total  # <-- need a return statement to send value back. A print is not sufficient