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

Make a function named add_list that takes a list.The function should then add all of the items in the list together.

Where am i getting it wrong please please assist

functions.py
def add_list(items_list):
  total = 0
  for item in items_list:
    total = total + item
    return total

3 Answers

cm21
cm21
150,854 Points

Hi Chipo,

It's an indentation issue (since Python is whitespace sensitive, code will not compile unless it is indented the amount that the compiler expects.) If you line the return statement up with the for loop declaration, your code will pass.

def add_list(items_list):
  total = 0
  for item in items_list:
    total = total + item
  return total

Hope this helps!

Hie CJ Thank you soo much you are a life saver.

sidni ahmed
sidni ahmed
3,329 Points

can someone please explain this. I don't get it. It says in the task it should return 6. Is this code returning 6?