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 trialJoel Escalante
Courses Plus Student 307 PointsCan't get code to run on test, but runs everywhere else...please help? Maybe I am not understanding problem?
def add_list(numbers):
num_list = numbers
total = sum(num_list)
print(total)
def summarize(numbers):
num_list = numbers
liststring = str(num_list)
total = sum(num_list)
totalstring = str(total)
print("The sum of " + liststring + " is " + totalstring + ".")
add_list([1, 2, 3])
summarize([1, 2, 3])
Joel Escalante
Courses Plus Student 307 PointsThank you! :)
2 Answers
Matthew Rigdon
8,223 PointsIn the test, they want you to return answers, not print them. Remember, printing something sends words to your screen, whereas using return is the "answer" from the function. Change both of your last lines to return and that should fix your issue. There is also a little bit of redundancy in your code (unnecessary lines) that you could remove if you want. Here is an example of what I did:
def add_list(items):
total = 0
for item in items:
total += item
return total
def summarize(lists):
total = add_list(lists)
newList = str(lists)
return("The sum of {} is {}.".format(newList, total))
Samuel Webb
25,370 PointsYour summarize
method is probably a bit more pythonic than mine. It's been a while since I've used Python.
Joel Escalante
Courses Plus Student 307 PointsThank you, I sincerely appreciate the help :)
Joel Escalante
Courses Plus Student 307 PointsCool...I had to look that one up...LOL you guys are great...appreciate the support.
Samuel Webb
25,370 PointsYou're over thinking it. For the first one they want you to use a for loop. For the second one all you need is a return value. Here's what I did:
def add_list(l):
output = 0
for li in l:
output += li
return output
def summarize(l):
return "The sum of " + str(l) + " is " + str(add_list(l)) + "."
Joel Escalante
Courses Plus Student 307 PointsThank you! :)
Samuel Webb
25,370 PointsNo problem. Glad to help.
Samuel Webb
25,370 PointsSamuel Webb
25,370 PointsProvided easier to view formatting for you.