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

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 68,423 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 68,423 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