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 trialisam elbousserghini
1,959 Pointshow do you add the items in the list?
how do you add items to the list?
# def add_list(list):
list = []
return total = list(0)
# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
# Note: both functions will only take *one* argument each.
2 Answers
akak
29,445 PointsHi Isam,
I don't have access to the challenge right now so I can't check if that's exactly what you are asked for, but this solution is more or less what you are looking for.
list = [1, 2, 3]
def add_list(list):
total = 0
for item in list:
total += item
return total
def summarize(list):
total = 0
for item in list:
total += item
return "The sum of {} is {}.".format(list, total)
Chris Freeman
Treehouse Moderator 68,441 PointsIt's best not to use reserved words, such as list
, as variable or arguments names. Common alternative is "lst".
Caleb Mulliner
425 Pointsusing total = 0 brought up an error saying the total equaled 1, so i changed the 0 to a 5 and got it back as correct. however, i'm honestly not sure why that is, also i don't know what the "+=" does
akak
29,445 Pointstotal += item is a shorter way of writing total = total + item.
total = total + item
total += item #does exactly the same as the previous one
The idea here is that you add each number to total.
total = 0, add 1
total = 1, add 2
total = 3, add 3
total = 6
I don't know why it doesn't work for you. Maybe the challenge wants to do it different way. Can't check that since my account is paused at the moment. Sorry.
Chris Freeman
Treehouse Moderator 68,441 Pointstotal += item
is shorthand for
total = total + item
"so i changed the 0 to a 5" does not seem correct. Can you repost your code?
Caleb Mulliner
425 Pointsi went back and changed it back to 0 and it worked fine, not sure why i had to make it a 5 the first time.
isam elbousserghini
1,959 Pointsisam elbousserghini
1,959 Pointsthank you