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

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

How to ask again for the index inside the new try statement

Hello guys.

I am new to python (and programming in general), but since this is something that I have already encountered (not only in python) I would like to discuss it.

As Kenneth Love suggests, I have inserted a try statement with its except for the case that spot = int(index)-1 returns an error.

It works enough good at first sight but it is not completed. With the simple code I have entered, the console (when interpreting the new except case) returns back to the very initial input, while I want to ask again for ONLY the index (keeping the list item lastly inserted by the user.

How should I add this thing?

I need something like going back again to the "index input".

Here is the code I have inserted so far:

    if index:
      try:
        spot = int(index) - 1
        for item in new_list:
          shopping_list.insert(spot, item.strip())
          spot += 1
      except:
          print("Not a valid number! Please enter the item again a valid number!")
    else:
      for item in new_list:
        shopping_list.append(item.strip())

As said it works fine but it is not enough.

Any ideas?

ty

Vittorio

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

So you want to keep the list they've given, but ask for a new number? I'd suggest moving the index-asking to a function and you keep asking for it until you're happy. Basically a new while loop inside everything else that keeps calling the get_an_index function you're going to write.

KAZUYA NAKAJIMA
KAZUYA NAKAJIMA
8,851 Points

Hello, I tried as below. if any missing, please let me know.

shopping_list = []

def show_help():
  print('\nEnter your items shop for, separated each item with a comma.')
  print('Type DONE to done, SHOW to see current list, HELP to see this message.')

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

print('Please input the list which conteins you want to buy for!')
show_help()

while True:
  new_stuff = input('> ')

  if new_stuff.upper() == 'DONE':
    print("\nHere's your list")
    show_list()
    break

  elif new_stuff.upper() == 'SHOW':
    print("\nHere's your list")
    show_list()
    continue

  elif new_stuff.upper() == 'HELP':
    show_help()
    continue

  else:
    new_list = new_stuff.split(',')
    show_list()
    while True:
      index = input('Do you want to add these items at the end of the list? Please press Enter.\n'
                      'Or give me a number you want to add items to. Currently {} items in the list.'.format(len(shopping_list)))

      if index:
        try:
          spot = int(index) - 1
          for item in new_list:
            shopping_list.insert(spot, item.strip())
            spot += 1
          break
        except:
          print('Please input appropriate content!')
          continue
      else:
        for item in new_list:
          shopping_list.append(item.strip())
        break