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

Frank Hoke
Frank Hoke
8,651 Points

Code challenge says I'm missing Y on summarize, but I'm not and the code runs

Any ideas why I'm getting a bummer on the code challenge? I have a y in the argument and the code runs and gives the right answer, I think.

test_list = [1,2,3,4]

def add_list(num_list):

for num in num_list: total = sum(num_list) return total

print(add_list(test_list))

Y=str(add_list(test_list)) X=", ".join(map(str, test_list))

def summarize(X, Y): return "The sum of " + X + " is " + Y

print(summarize(X,Y))

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.
test_list = [1,2,3,4]

def add_list(num_list):

  for num in num_list:
    total = sum(num_list)
  return total

print(add_list(test_list))

Y=str(add_list(test_list))
X=", ".join(map(str, test_list))

def summarize(X, Y):
  return "The sum of " + X + " is " + Y

print(summarize(X,Y))

3 Answers

Bart Bruneel
Bart Bruneel
27,212 Points

Hello Frank,

I think the answer here depends on several things:

  • The list shouldn't be joined to create a string. You can create a string of the whole list like this: str(test_list). This should be done inside the summarize function (see below)

  • Second, note how the description says both functions should just take one argument. This means that in the summarize function only the whole list needs to be passed in. In this summarize function you can call add(list) to sum the list and assign this sum to a new variable named Y. Then assign the lists itself to X. These variables can then be passed into the string. Like this: "The sum of" +str(X) +"is" +str(Y).

  • All the print statements should be removed. Even the summarize function should only return the string.

  • In the add function a for-loop can be written to loop over the list and add every element of the list to a running total, like this:

total=0
for element in list:
  total+=element
return total

or you can return sum(list). The for-loops now loops over every element in the list, but assign the same number to total each time, namely the sum of the whole list.

Happy coding!

The for loop inside add_list isn't doing anything. I'm not sure whether this is causing your error, but it is causing the list to be summed repeatedly. The instructor's intent was to have students iterate over the values and add each to a total, while you've chosen to use the sum() function. That's fine to do, but you don't need to nest it inside a loop. The sum function is what they're trying to build in this challenge.

Frank Hoke
Frank Hoke
8,651 Points

Thanks Bart and Jack. That worked.