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

Frank Sherlotti
PLUS
Frank Sherlotti
Courses Plus Student 1,952 Points

"SHOW" is displaying nothing.

shopping_list = []

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

def show_list():
  count = 1
  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("\nHere's your 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 certain spot? Press enter at the of the list, "
                 "or give me a number. Currently {} items in the list.".format(len(shopping_list)))
    if index:
      spot = int(index) - 1
      for item in shopping_list:
        shopping_list.insert(spot, item.strip())
        spot += 1
    else:
      for item in shopping_list:
         shopping_list.append(item.strip())

I can't figure out what is wrong with this.. I've cycled through the video like 3 times and it seems right to me.

2 Answers

Michael Norman
PLUS
Michael Norman
Courses Plus Student 9,399 Points

"SHOW" is not displaying the list because you are not inserting anything into the list. In the for loops below you are asking " for each item in the shopping list append that item to the shopping list", but we start out with an empty shopping list.

shopping_list = []

# . . . 
# . . . 

 new_list = new_stuff.split(",")
    index = input("Add this at a certain spot? Press enter at the of the list, "
                 "or give me a number. Currently {} items in the list.".format(len(shopping_list)))
    if index:
      spot = int(index) - 1
      for item in shopping_list:
        shopping_list.insert(spot, item.strip())
        spot += 1
    else:
      for item in shopping_list:
         shopping_list.append(item.strip())

What you need to do instead is take the user input, which is the line.

new_list = new_stuff.split(",")

and loop through each item and append it to the shopping list. The corrected code looks like this.

shopping_list = []

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

def show_list():
  count = 1
  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("\nHere's your 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 certain spot? Press enter at the of the list, "
                 "or give me 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())
        spot += 1
    else:
      for item in new_list:
         shopping_list.append(item.strip())
Alexander Kallaway (Emelianov)
Alexander Kallaway (Emelianov)
10,307 Points

I seem to have the exact same code as the fixed version, but it doesn't work. Could you please help me spot the problem?

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

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(",")
    index = input("Add this at a certain spot? Press enter for the end of the list, "
                  "or give me 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())
        spot += 1
      else:
        for item in new_list:
          shopping_list.append(item.strip())
Alexander Kallaway (Emelianov)
Alexander Kallaway (Emelianov)
10,307 Points

I've figured it out - my else statement indentation in the very end of the script was off. :)

I dont see what you changed in your code, can you please explain?

Alexander Kallaway (Emelianov)
Alexander Kallaway (Emelianov)
10,307 Points
# In the very end of the script, the last if statement:
if index:
    spot = int(index) - 1
    for item in new_list:
        shopping_list.insert(spot, item.strip())
        spot += 1
else:  # this else statement and its contents have to be moved one indentation to the left,
# to match with the if part of the condition
    for item in new_list:
    shopping_list.append(item.strip())