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

if we answer != int at spot then it gives and error and i couldnt fixed it by try/except can u show me the way pls

shopping_list = []


def show_help():
  print("\nSeperate items with a comma.")
  print("Press DONE to quit, SHOW to see your current shopping list and HELP to see this.")


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

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

while True:
  new_stuff = input("> ")

  if new_stuff == "DONE":
    print("\nHere is your shopping 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 item to a certain place? Press Enter to add at the end, "
                  "or give me a number. Currently {} items are on 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())

--- I try to put try/except before (if index:) line but cant make it work . I only want to make a proper way out when we write anything but != int at the answer of spot .

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

I've added a try/except to your code as shown:

#!/usr/bin/python
shopping_list = []

def show_help():
    print("\nSeperate items with a comma.")
    print("Press DONE to quit, SHOW to see your current shopping list and HELP to see this.")

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

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

while True:
    new_stuff = input("> ")

    if new_stuff == "DONE":
        print("\nHere is your shopping 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 item to a certain place? Press Enter to add at the end, " "or give me a number. Currently {} items are on the list: ".format( len(shopping_list)))

    try:
        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())
    except ValueError:
        print("Input index {} is not allowed.".format(index))

Which gives the following output:

$ python3 tmp.py 
Give me a list of things that you want to shop.

Seperate items with a comma.
Press DONE to quit, SHOW to see your current shopping list and HELP to see this.
> a
Add this item to a certain place? Press Enter to add at the end, or give me a number. Currently 0 items are on the list: r
Input index r is not allowed.
> DONE

Here is your shopping list.

Thanks for the answer. it solved the error. Thanks a lot...