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

Putting the "Fun" Back in "Function" add_list didn't return the right answer. Got 3 when adding [1, 2, 3], expected 6.

I'm stuck. Tried exactly the same code on a Python console in my computer with the same Python version with different result as follows: 1 2 3

Sorry, I know people already asked this questions but answers were not very helpfful for my case. Already tried return instead print but nothing. Help please! =)

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.
mylist = (1, 2, 3)

def add_list(mylist):
  for i in mylist:
    total =+ i
  return total

def summarize(mylist):
  total = add_list(mylist)
  print ("The sum of [1, 2, 3] is {}.".format(str(mylist), total + "."))

2 Answers

Michael Williams
Michael Williams
13,005 Points

Hi Israel Clavé,

The following may be helpful. A couple differences include declaring a value for the variable "total" before beginning the for loop and swapping the order of the "=+" to "+=".

def add_list(my list):
  total = 0
  for i in mylist:
    total += i
  return total

Best Regards, Michael Williams

Ooops! Thanks a lot Michael Williams! That solved my problem! Take care!

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You also need to return from the summarize() function, not print().

Awesome! Thank you! =)