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

Python Basics, Stage 5

Make a function named add_list that adds all of the items in a passed-in list together and returns the total. Assume the list contains only numbers. You'll probably want to use a for loop

my code:

the_list = [] def add_list(): for item in the_list

please point me in the right direction...

2 Answers

Charlie Thomas
Charlie Thomas
40,856 Points

def add_list(passed_in_list): total = 0 for number in passed_in_list: total = total + number return total

What you are doing is defining a function called add_list, then giving it one argument called passed_in_list. Then you need to create a variable called total to store the total of the numbers. Then you create a for loop and go through every item in the list and update the total, the return the total variable

Thanks, that makes sense :)

Charlie Thomas
Charlie Thomas
40,856 Points

Ever need anymore help just ask :)