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

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
229,005 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()