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 trialKhaleel Hamid
5,258 PointsCode challenge 1 of 2 error
I have this code, which should be correct and I looked at some other people who I asked the same question and matched one with theirs, but it still gives me an error
def add_list(items):
total = 0
for num in items:
total += num
return total
Ron McCranie
7,837 PointsYou're very close. Take a look at your loop, you're saying for each num
in the items
array (that part is good). Then you're saying to add the value of items
to the existing variable called total
, think about that part there.
Ron McCranie
7,837 PointsLooks like you self corrected the original post. What you have up there now works for me.
Khaleel Hamid
5,258 PointsInteresting. It still gives me an error.
Ron McCranie
7,837 Pointsyour original code was:
def add_list(items):
total = 0
for num in items:
total += items
return total
You now have:
def add_list(items):
total = 0
for num in items:
total += num
return total
You're trying the second one, right?
Khaleel Hamid
5,258 PointsYes, I am. I initially had that code and was retyping in when making this thread and accidentally put in items. I have been using the second one the whole time.
Khaleel Hamid
5,258 PointsI got. I removed the comment add_list which is why I got the error.
Ron McCranie
7,837 PointsYou might want to go ahead and summarize what you did that made it work in an answer post so you can close the question out.
1 Answer
Khaleel Hamid
5,258 PointsWell my mistake was that I had this code that was commented out included in my challenge. I thought I need it in order to make the function and make it work.
add_list([1, 2, 3]) should return 6
Once I commented it back out
# add_list([1, 2, 3]) should return 6
The code worked.
Khaleel Hamid
5,258 PointsKhaleel Hamid
5,258 Points.