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

Please explain the method of inserting an item at a specific position in the list

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

In the if-else statement in the above function, if we set the position =0,when there are no items in the list, how the item will be added to the list using the statement: shopping_list.insert(position-1, new_item)..

Won't it make the position negative again!! Please exlplain

1 Answer

How can i insert the snapshot from my atom editor for the code to this discussion?