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

function task add_list

help please guys, i have been stuck on this task forever, why is my function returning 1

functions.py
# add_list([1, 2, 3]) should return 6
# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
# Note: both functions will only take *one* argument each.
def add_list(calculator):
  my_counter = 0
  for item in calculator:
    my_counter  += item
    return my_counter
def summarize_list(calculator):
    print("The sum of {} is {}.".format(calculator, add_list(calculator)))
calculator = [1, 2, 3]
summarize_list(calculator)

1 Answer

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Dephine.

The indentation of the return statement is wrong in the add_list function definition.

With the indentation you provided the function returns the total at the end of the first loop cycle.

Try setting, on the return line, the same indentation of the the for loop line.

This should work for you, you are very close to the right solution and then you can move on to the next task.

Let me know if still struggling.

Vittorio

thanks dude

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Good!

Please remove the other question as it is a duplicate of this one.

;)

Vittorio