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 trialErik Courtney
2,543 PointsFunctions add_list and summarize work everywhere else but your code interpreter.
in challenge task 2 of 2, I'm unable to get two functions to execute properly. This code and minor variations I have tried will run on several other interpreters I've tried. Any ideas?
# 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(items):
total = 0
for i in items:
total = total + i
return total
def summarize(lst):
ntotal = add_list(lst)
print("the sum of %s is %s." % (lst, ntotal))
3 Answers
Erik Courtney
2,543 PointsThanks, I think it was looking for a return and not a print. Though the error AttributeError: 'NoneType' object has no attribute 'lower'" didn't really help on that front.
Replacing print with return worked fine with original code as well.
Tree Casiano
16,436 PointsIn the summarize function, I was able to get the code to pass by using string formatting, like so:
def add_list(items):
total = 0
for i in items:
total = total + i
return total
def summarize(items):
return "The sum of {} is {}".format(str(items), sum(items))
I'm not sure why your original code would run elsewhere and not here unless it has to do with different versions of Python.
Kenneth Love
Treehouse Guest TeacherYeah, you have to return
from functions in code challenges (really, you'll probably want to do this 99.9999% of the time anyway). The NoneType
error comes from the fact that I got nothing back from your function (or, rather, I got None
) which doesn't have a .lower()
method.
Tree Casiano
16,436 PointsTree Casiano
16,436 PointsThat make sense. Glad you got it to work.