Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Lists Shopping List Take Three

Darian Garcia
Darian Garcia
1,823 Points

If user enters 0, exception is not caught, position is not none and element is inserted at element [-1]

This is a mistake and should be fixed. According to the instructor we don't want to do negative indices.

3 Answers

Steven Parker
Steven Parker
215,972 Points

Good point! You might want to report this as a bug to the Support folks. They might add something to the Teacher's notes, and you could get an "exterminator" badge. :beetle:

Podrig Leoghain
Podrig Leoghain
5,094 Points

Doesn't appending an item to a list at position -1 append that item to the end of the list?

EDIT: Wait, just tried it and it will insert (silly me) item in position -1, i.e. in position occupied by current last item. Yup, you caught a bug!

Second edit: Have tried a while loop but I'm not clever enough yet to work this one out. Sorry!

Try this: The the function def add_to_list, evaluate the position variable after deriving the abs value. Using the if control statement, when the position variable evaluates to zero, then assign it to None. This will cause the item to be appended to the list when zero is entered.

def add_to_list(item): show_list() if item in shopping_list: print("Item already added") else: if len(shopping_list): 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)) if position == 0: position = None except ValueError: position = None if position is not None: shopping_list.insert(position-1,item) else: shopping_list.append(item) show_list()