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

This stuff is broken

How come the perfectly working implementation of this silly little quiz that I just wrote with vim and ran at the command line keeps getting rejected by the app?

Bah!

functions.py
# numlist = []
#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.

numlist = [1, 2, 3]
def add_list(numlist):
    sum(numlist)

add_list(numlist)

Hi Josh, Thanks for the reply. Actually, this isn't my code, just the half baked stuff that was in the buffer on their editor. As I stated, I have a working implementation of both functions that fulfills the problem statement, that runs just fine at the command line. I switched over to writing the file in vim and running it when the web interface kept eating my code...

numlist = [1, 2, 3] 
def add_list(numlist):
#   numlist = [1, 2, 3]
#   tot = 0
#   for num in numlist:
#       tot += num
#   print(tot)
    print(sum(numlist))

def summarize():
    print("The sum of the values in the array is {}".format(sum(numlist)))


add_list(numlist)

summarize()

As you can see, I've written it with both the built-in function and a for loop. It appears that the game is to deduce what they are looking for.

Best S

[MOD: added ```python formatting -cf]

2 Answers

Josh Keenan
Josh Keenan
19,652 Points

There are a few problems here. Firstly, you don't return anything! Return the answer in the format required, then it should work!

Josh Keenan
Josh Keenan
19,652 Points
# 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(list1):
  total = 0
  for item in list1:
    total += item
  return total

def summarize(list2):
  total = 0
  for item in list2:
    total += item
  return "The sum of {} is {}".format(list2, total)

This is the correct answer, yours doesn't pass as you don't meet the requirements of the challenge. Firstly, you should be using the return statement instead of the print statement; secondly, numlist is something put in between the brackets when the function is called, if you define it in the function it will never work, only return the sum of the list you have now built into it.

You also should delete the variable declared before 'add_list' as it isn't needed; finally, your formatting in summarize is wrong and you don't need to call either function! Hope this helps :)