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

Luke Telford
Luke Telford
2,931 Points

How to use for loop?

I can't wrap my head around this, in the challenge it says to sue a for loop to run both these functions but i don't remember anything being said about for loops during this course. How do I go about doing this?

1 Answer

Hi Luke

this is how you want to do it.

def add_list(mylist):
  total = 0
  for num in mylist:
    total = total + num

  return total

You pass a list to the function for example my_list = [1,2,3] which should return 6 when the function add_list() is called.

Luke Telford
Luke Telford
2,931 Points

Thank you! this was a great help