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 trialBen Altieri
4,645 PointsCode tested successfully in Workspaces and PythonAnywhere. Why does the Functions Code Challenge Quiz step 2 error out?
The error I get for the subsequent code is as follows in the Quiz step 2: "Bummer! AttributeError: 'list' object has no attribute 'lower'"
--------------
def add_list(list_1): sum = 0 for item in list_1: sum = sum + item return sum
def summarize(list_1): sentence = "The sum of X is Y" word_list = sentence.split() word_list[3] = str(list_1) word_list[5] = add_list(list_1) return word_list
list_1 = [1, 2, 3, 4, 5] report = summarize(list_1) print(report)
# 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(list_1):
sum = 0
for item in list_1:
sum = sum + item
return sum
def summarize(list_1):
sentence = "The sum of X is Y"
word_list = sentence.split()
word_list[3] = str(list_1)
word_list[5] = add_list(list_1)
return word_list
list_1 = [1, 2, 3, 4, 5]
report = summarize(list_1)
print(report)
4 Answers
Stone Preston
42,016 PointsSummarize needs to return a string, however it is currently returning a list. join the list with spaces to make it a string. also you need to convert the return value of add_list to a string as well (you did this on the line above for list_1, but the sum needs to be a string too):
# 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(list_1):
sum = 0
for item in list_1:
sum = sum + item
return sum
def summarize(list_1):
sentence = "The sum of X is Y"
word_list = sentence.split()
word_list[3] = str(list_1)
word_list[5] = str(add_list(list_1))
return " ".join(word_list)
also dont call the function in the challenge, it does not ask you to do that
a simpler way would be to use .format which makes the whole function one line, so thats nice
def summarize(list_1):
return "The sum of {} is {}".format(list_1, add_list(list_1))
Mikael Enarsson
7,056 PointsYou are returning a 'list', you should return a a 'string'. Also, this is a really cumbersome way to do it, it would be better to use the '.format()' method:
the_number = 6
"Your string, and the number {}.".format(the_number)
or you could use string concatenation:
the_number = 6
"Your string, and the number " + str(the_number) + "."
Ben Altieri
4,645 PointsThank you Stone and Mikael. You were right. I used Stone's summarize function to get past it. I have saved the code in my PythonAnwhere account. Going to remember this one. Cost me a couple late nights before finally breaking down and asking the forum.
Thank you both for your help.
Cheers, - Ben
Kenneth Love
Treehouse Guest TeacherSorry you had trouble with this! But, thank you for finding this situation. I've updated the CC so it'll give a better error message if someone returns a list instead of a string from summarize()
.
Ben Altieri
4,645 PointsIts all part of the struggle ... No worries. Hoes that saying go? It's not the destination but the journey ... Anyway, I'm committed to learning and using Python and your course is helping me achieve this objective.
Cheers,
- Ben