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 Shopping List Take Three

Getting many errors in my code,like"Traceback (most recent call last):"

Can anyone help me to debug this program? and please tell me the logic behind this code.

    shopping_list = {}


def show_help():
  print("\nseprate each iteam with a comma")
  print("Type SHOW to show your current list, type DONE when you are finished and HELP to show this \n massage")


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



print("Give me a list of things you want to shop for")
show_help()

while True:
  new_stuff = input("> ")
  if new_stuff == "DONE":
    print("\nheres 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(",")
    index = input("Add this at a certen spot: press enter for the end of the list"
                 "or give a number, currently {} items in the list".format(len(shopping_list)))
  if index:
      spot = int(index) - 1
      for item in new_list:
        shopping_list.insert(spot, new_list.strip())
        spot += 1
  else:

    for item in new_list:
      shopping_list.append(item.strip())

      ```

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Ignoring the PEP-8 styling issues, I found 4 errors in your code. See annotated code below:

shopping_list = []  # <-- #1 changed to list instead of dictionary

def show_help():
  print("\nseprate each iteam with a comma")
  print("Type SHOW to show your current list, type DONE when you are finished and HELP to show this \n massage")

def show_list():
  count = 1  # <-- #2 moved count init outside of loop
  for item in shopping_list:
    print("{}: {}".format(count, item))
    count+=1


print("Give me a list of things you want to shop for")
show_help()

while True:
  new_stuff = input("> ")
  if new_stuff == "DONE":
    print("\nheres your list")
    show_list()
    break
  elif new_stuff == "HELP":
    show_help()
    continue
  elif new_stuff == "SHOW":
    show_list()  # <-- #3 added missing parens
    continue
  else:
    new_list = new_stuff.split(",")
    index = input("Add this at a certen spot: press enter for the end of the list"
                 "or give a number, currently {} items in the list".format(len(shopping_list)))
  if index:
      spot = int(index) - 1
      for item in new_list:
        shopping_list.insert(spot, item.strip())  # <-- #4 strip item instead of new_list
        spot += 1
  else:

    for item in new_list:
      shopping_list.append(item.strip())