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 trialIbrahim Mahmoud
714 PointsI am getting a persistent error...?
I am getting an error saying "Attribute Error: "NoneType" has no attribute "lower" " and i have no idea why or where the error is coming from. Anyone know what I am doing wrong?
# 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):
sum = 0
for item in list:
sum += list[item - 1]
return sum
def summarize(list):
print("The sum of " + str(list) + " is " + str(add_list(list)) + ".")
summarize([int(1),int(2),int(3)])
1 Answer
Mikael Enarsson
7,056 PointsThey want you to return the string, not print it ^^
There is also a pretty mayor problem with your add_list function:
def add_list(list):
sum = 0
for item in list: #The problem starts here. What this for statement does is, it takes the
#value of each index in the list, and performs the operations on it.
#for example, if you pass in [1, 2, 3], it performs the operations
#once for 1, once for two and once for three
sum += list[item - 1] #Here, you are accessing the value at index item - 1 in the list.
#So, in the case with the list from the previous example,
#they would be list[0], list[1] and list[2], which is fine
#(and apparently what they pass in for this challenge).
#Now, the problem would occur if you, for example, passed in
#the list [3, 4, 5]. In this example, they would attempt to access
#list[2], list[3] and list[4]. As you may notice, the last two are
#out of range a for this array (since three items means that the
#indices range from 0 to 2.
return sum
Indeed, if you attempt to pass the list in the second example, you get IndexError: list index out of range. What you want to do is take the item, which is the value at the point currently being processed by the loop, and add it to sum:
def add_list(list):
sum = 0
for item in list:
sum += item
return sum
Sorry if this was a bit verbose, I tend to get a bit longwinded ^^' Anyway, I hope that it's helpful!
Ibrahim Mahmoud
714 PointsThanks xD this saved me another week of stress xD
Mikael Enarsson
7,056 PointsNo problems ^^ Unfortunately, there are also a major problem with your add_list function that I didn't think I would have time to go into right now, but I have some more time (than I thought, so I'll make an edit about it).
Ibrahim Mahmoud
714 PointsOk thanks
Mikael Enarsson
7,056 PointsNow, I don't know if updates to the posts give messages, so, I'm asking this, did you get a message about 3~4 hours ago about my update?
Yeeka Yau
7,410 PointsThanks for this answer - I was confused by this simple thing as well - coming from Java - it seems like it works in the opposite way in python, my understanding here is that the for loop already accesses the value of each index in the list - rather than specifying the index and then accessing the value at that index in the loop.
Ibrahim Mahmoud
714 PointsIbrahim Mahmoud
714 PointsOk I got it now thanks! I understand now. yes i get updates xD