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

Python Python Collections (Retired) Lists Redux Removing Items From A List

Can someone please tell me why this code is not adding items to the shopping list? It always has 0 items

#purpose: add itmes to shopping list. Choose index of item on list

shopping_list = []

def show_help():
  print("\nSeparate each item with a comma.")
  print("Type DONE to quit, SHOW to see the current list, and HELP to get this message.")


def show_list():
  count = 1
  for item in shopping_list:
    print("{}: {}".format(count, item))
    count +=1

#code officially begins    
print("Give me a list of things you want to shop for.")
show_help()


while True:
  new_stuff = input("> ")

  if new_stuff == "DONE":
    print("\nHere's your list:")
    show_list()
    break
  elif new_stuff == "HELP":
    show_help()
    continue
  elif new_stuff == "SHOW":
    show_list()
    continue
  else:
    new_list = new_stuff.split(",") #turns string of words separated by comma into list of strings
    index = input("Add this at a certain spot? Press enter for the end of the list, or give me a number. Currently {} items in list.".format(len(shopping_list)))
    if index: #if there is an index
      spot = int(index)-1 
      for item in new_list:
        shopping_list.insert(spot, item.strip())#strip item of an whitespace characters around it
        spot +=1
      else:
        for item in new_list:
          shopping_list.append(item, strip())

Added markdown to python code

What do you mean? Thanks.

There is a link near the bottom of this page called Markdown Cheatsheet which, among other things, explains how to display your code with proper syntax highlighting. As a mod, I'm able to edit your post to add this markdown, so that your code will be easier to read - giving you faster, more accurate help.

Gocha. Thanks for the tip!

3 Answers

Hi Otarie,

There are two problems that need fixing. The first, pointed out by Kevin Franks is that the final else statement is indented too far. The second issue is the last line:

shopping_list.append(item, strip())

# should be changed to

shopping_list.append(item.strip())

With those changes, your code works for me.

Cheers

Kevin Franks
Kevin Franks
12,933 Points

I believe your last else statement is indented to far over and therefore not being executed.

Thanks guys - all fixed!