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

Martel Gaines
Martel Gaines
1,957 Points

Function Challenge Task 1 of 2

Hi Python community,

Can I have someone provide any insight on this challenge involving returning a list of numbers that equals 6 in a list? I've already defined my list with a num argument and I was in the process of using the "for" loop as an index to sum the numbers together, but I keep getting a syntax error. Any advice would be appreciated!

Martel

functions.py
def add_list(num):
  for i = 1:1:7

1 Answer

Dan Johnson
Dan Johnson
40,533 Points

The return of 6 was just a specific example in the case that the list [1, 2, 3] was passed in as the argument. What the challenge wants you to do is provide a generic solution to summing all numbers in a list. The list will be provided when your code is being checked.

To start, you'll want a variable local to the function that will keep track of the sum. Then you will want to use a for loop to iterate over all the numbers in the list. The for loop follows the pattern:

for item in collection:
  #...

So in order to get all the numbers out of the list and add them you'll want to do something like this:

for number in number_list:
  #Update sum here.

Then finally return the sum.