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

Michael Escoto
Michael Escoto
30,028 Points

Shopping List App Not Working

shopping_list = []


def show_help():
 print("\nSeperate 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())

If I enter in items into the shopping list and don't specify an index and then type in SHOW it doesn't display the items in my list.

If I do specify a index it will duplicate the number of items I entered in. I'm not sure where I'm going wrong.

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

It looks like your else, at the end, is indented the same amount as your for instead of the if that it corresponds to.

Michael Escoto
Michael Escoto
30,028 Points

That was it. Thank you very much for your help.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You're very welcome, Michael Escoto !

It helps a bit if you change the workspaces indentation from 2 to 4. Makes indentation alignment a little easier to see.

Evaluating the code below in the treehouse workspace reproduces the behaviour seen in the video for this lesson, but somehow, evaluate this as a buffer in python mode in emacs, or in sublime text python mode, or indeed by doing python shopping_list_3.py from the terminal, gives the following error:

>>> Give me list items.

Separate each item with a comma.
Type DONE to quite, SHOW to see current list, and HELP to get this message.
> bla, foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/david/Dropbox (Personal)/Learning/TreeHouse/Python/Collections/shopping_list_3.py", line 17, in <module>
    new_stuff = input("> ")
  File "<string>", line 1, in <module>
NameError: name 'bla' is not defined

here's the code I'm trying...any thoughts?

shopping_list = []

def show_help():
    print("\nSeparate each item with a comma.")
    print("Type DONE to quite, 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 list items.")
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 spot? press enter for end of list,"
                        "or give me a number. Currently {} items in 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())
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Dollars to doughnuts, you're running it against Python 2 instead of 3 if input is giving you that error. Upgrade to 3 or swap in raw_input locally.

Yessir! That was -exactly- what was wrong. Now, to figure out how to upgrade to Python 3! I'm trying to learn a bit of emacs while I do your Python track (this -is- turning out to be a track, no? (pretty please?)).

Thank you for such a quick reply!