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

Sai Rupa Vallabhaneni
Sai Rupa Vallabhaneni
970 Points

"IndexError: tuple index out of range error" for shoppingList.py problem

I was trying to run the shoppingList.py program and running into the below issue when I try to add more than 1 item in the list:

banana
Items in your list are

1 - apples

Traceback (most recent call last):
File "shoppingList.py", line 63, in <module>
addToList(new_item)
File "shoppingList.py", line 24, in addToList
"> ".format(item))
IndexError: tuple index out of range

Here is what I have in the addToList() function:

def addToList(item): showList() if len(shoppingList): position = input("Where should I add the {} \n" "Press ENTER to add {} at 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: shoppingList.insert(position-1, item) else:
shoppingList.append(item)

not able to figure out what I was missing here, any help appreciated. Thanks

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

You can format your code by checking the Markdown Cheatsheet below :arrow_heading_down:

Looks like the error message is pointing to your input statement.

I would start by verifying the quotes and ensure that you are passing in an argument in the format for each placeholder.

Missing an argument in the format could cause an index out of range exception.

position = input("Where should I add the {} \n" "Press ENTER to add {} at the end of the list \n" "> ".format(item))
Sai Rupa Vallabhaneni
Sai Rupa Vallabhaneni
970 Points

The issue was I was using to placeholders {} in the input statement and only passing 1 argument in the format. updating it fixed the issue. Thank you