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!

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


whats wrong in my code. preview is just but a black screen.

def add_list(num_list)
    for num in num_list:
      return num+num

# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
# Note: both functions will only take *one* argument each.

2 Answers

Dan Johnson
Dan Johnson
40,532 Points

Functions can only return once. When you use return you exit out of the function right at that point. In order to sum up the list of numbers using a for loop you'll need to use a local variable:

def add_list(number_list):     #Function signatures need to end with a colon to start the block.
  total = 0     
  for number in number_list:
    total += number

  return total     #Everything in the same block should have consistent indentation.

You don't need to call the add_list function yourself for this challenge. But if you did, you'd need to supply it with a list of numbers and also remove the indentation as it currently resides in the function definition.

Thanks for the help! It really worked.