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

It should return the string "The sum of X is Y.", replacing "X" with the string version of the list and "Y" with the sum

What is wrong with this code? I see in the interpreter that I am getting an INT but it's expecting a string. Doesn't join() make this into a string by default?

def add_list(my_list):
  sum = 0
  for _ in my_list:
    sum += _
  return sum

def summarize(list_two):
  list_sum = add_list(list_two)
  string_list = ' '.join(list_two)
  return "The sum of {} is {}".format(str(string_list), list_sum)

print(summarize([1,2,3]))
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

string_list = ' '.join(list_two) is throwing the error because list_two contains numbers, which can't be joined into a string without converting them to strings first. Try taking that line out completely, and change your format() to only use list_two and list_sum, and see what happens.

2 Answers

This is what I used to pass the challenge

def add_list(numbers):
  total = 0;
  for number in numbers:
    total = total + number
  return total

def summarize(numbers):
  return "The sum of {} is {}.".format(numbers, add_list(numbers))

[Edit] sorry what I ended with was the return on the 2nd function I just hadn't changed it in my work file.

Thanks, that helped a lot.

cool can you make it "best answer"?