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 trialEdnalyn De Dios
20,259 PointsTypeError: unsupported operand type(s) for -: 'str' and 'int'
Got this error when I tried to add at the end of the list by pressing ENTER:
Traceback (most recent call last):
File "shopping_list_3.py", line 65, in <module>
add_to_list(new_item)
File "shopping_list_3.py", line 31, in add_to_list
shopping_list.insert(position-1, item)
TypeError: unsupported operand type(s) for -: 'str' and 'int'
import os
shopping_list = []
def clear_screen():
os.system("cls" if os.name=="nt" else "clear")
def show_help():
clear_screen()
print("What should we pick up at the store?")
print("""
Enter 'DONE' to stop adding items.
Enter 'HELP' for this help.
Enter 'SHOW' to see your current list.
""")
def add_to_list(item):
show_list()
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))
except ValueError:
position == None
if position is not None:
shopping_list.insert(position-1, item)
else:
shopping_list.append(item)
show_list()
def show_list():
clear_screen()
print("Here's your list:")
index = 1
for item in shopping_list:
print("{}. {}".format(index, item))
index += 1
print("-"*10)
show_help()
while True:
new_item = input("> ")
if new_item.upper() == 'DONE' or new_item.upper() == 'QUIT':
break
elif new_item.upper() == 'HELP':
show_help()
continue
elif new_item.upper() == 'SHOW':
show_list()
continue
else:
add_to_list(new_item)
show_list()
2 Answers
Ryan S
27,276 PointsHi Ednalyn,
Nice work with the shopping list application. It is just one small syntax error that is causing your problem.
In your except
block in add_to_list()
, you are using an equality conditional expression, rather than an assignment operator.
When you hit Enter without inputting any value, you have assigned position
to be an empty string. When the ValueError is thrown, it is not getting assigned to None, rather it is evaluating an equality comparison. So then it moves on to the .insert()
statement (since position is not None) and tries to subtract 1 from an empty string.
def add_to_list(item):
show_list()
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))
except ValueError:
position = None # Remove one equal sign
if position is not None:
shopping_list.insert(position-1, item)
else:
shopping_list.append(item)
show_list()
Ednalyn De Dios
20,259 Points@Ryan S AHHH! Thank you mucho!!! Pesty little bugs. LOL