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 trial
Devin Bartley
2,800 PointsAdd list challenge - please help!
Hello,
I have been struggling for many hours with the add list challenge:
http://teamtreehouse.com/library/python-basics/putting-the-fun-back-in-function/functions
I feel like this should be basic, but its really causing me a lot of grief. I went back and reviewed all the modules and still am not connecting the dots. Here are a few approaches I have tried to just get the first part of the challenge done. Can anyone help point me in the right direction?
Here is the first way I tried it - it allows me to create a list and it even adds it, but its an executable script and if i type it into the shell and then write "add_list([1, 2, 3])" it only gives me errors:
list = []
def add_list(thing): list.append(int(thing)) print(thing)
def add_to_list(item): list.append(int(item)) print("Added! list has {} items.".format(len(list)))
while True:
item = input()
if item == 'DONE':
break
add_to_list(item)
continue
g = sum(list) print(g)
This didn't work so I thought maybe I was overcomplicating things. I then tried this (still no luck)
list = []
def add_list(thing):
for thing in add_list:
list.append(int(thing))
g = sum(list) print(g)
If someone could help me out or even point me in the best way to go about this simple task that would be awesome!!! My apologies for how this script looks in the forum - it is arranged correctly in my module with the correct indentation and spacing, it just doesn't paste well into this forum.
Thanks,
2 Answers
Tatiana Jamison
6,188 PointsHey Devin - you don't need input() at all for this. When I went through it, I defined the list to just include the items 1, 2, 3, since that was the example.
Your formatting is hard to read, but it looks like your code was:
list = []
def add_list(thing):
for thing in add_list:
list.append(int(thing))
g = sum(list) print(g)
Remember that indentation matters a lot, so that the following two things are different in Python:
def add_list(thing):
for thing in add_list:
list.append(int(thing))
g = sum(list) print(g)
def add_list(thing):
for thing in add_list:
list.append(int(thing))
g = sum(list) print(g)
Try playing around with it in Workspaces to see.
Next: add_list is the function. It's not an iterable like a list. What you want to say is: for item in thing. "thing" is the object that the function add_list is working with; "item" is the name you're calling any single piece in the iterable thing. You could also do "for number in thing" or any other naming convention you like.
I've found that code challenges don't provide very helpful feedback. If you get stuck on a challenge, try running your code in Workspaces.
Devin Bartley
2,800 PointsHey Tatiana,
I got it! Thanks so much for replying. I was WAY overthinking this. It ended up being a simple defining of a variable named list, and then defining the function add_list. Inside the function add list i had it return the sum of list. That was it!
Regards,