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 (2016, retired 2019) Lists Shopping List Take Three

Luke Maschoff
Luke Maschoff
820 Points

Shopping List trouble

When Kenneth started to add new code to the shopping list, I nearly understood all of it, but I may have missed something, but it seems like I have the same code as him?

Whenever I start the application, it runs, but when I add my first item, instead of automatically putting it in the first position, it asks me where I want to put it, messing everything up. Here is the section where I think it went wrong.

Whenever I press enter to add it to the end of the list as well, it adds another version of the item. ex: If I add apples, it will ask what place, or press enter to add it to the end of the list, and if I press enter it adds 2 to the list, so it would look like

  1. Apples
  2. Apples
def add_to_list(item):
    show_list()
    if len(shopping_list) >= 1:
        position = input("Where should I add {}?\n" 
                         "Press ENTER to add to the end of the list\n"
                         "> ".format(item))
    else:
            position = 0
    try:
        position = abs(int(position))
    except ValueError:
        position = None
    if position is not None:
        shopping_list.insert(position-1, item)
    else:
        shopping_list.append(new_item)

    show_list()

Can you post a snapshot of your workspace? It is the camera icon in the upper right corner.

1 Answer

You have an extra add_to_list(new_item) at line 85. Remove that and the rest appears to be ok.