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
Mohammad Aslam
6,053 PointsProblem with my Shopping_list_3.py code. Getting an error message when I run it.
I've copied most of the code from the video instructions but I am getting an error message, when I run it and try to add an item to the list. Can some one please help?
I am pasting the error message first and then the code.
Here's your list:
Traceback (most recent call last):
File "shopping_list3.py", line 68, in <module>
add_to_list()
File "shopping_list3.py", line 32, in add_to_list
shopping_list.insert(position-1, item)
NameError: name 'item' is not defined
import os
make a list to hold onto our items
shopping_list = []
def clear_screen(): os.system("cls" if os.name == 'nt' else 'clear')
def show_help(): clear_screen() # print out instructions on how to use the app print("What should we pick up at the store?") print(""" Enter 'DONE' to stop adding items. Enter 'HELP' to show these help lines. Enter 'SHOW' to show items added so far.""")
def add_to_list(): show_list() if len(shopping_list): position = input("Where should I add {}?/n" "Press ENTER to add to the bottom 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) Line 32!!!!!!!!
else:
shopping_list.append(new_item)
show_list()
def show_list(): clear_screen()
# print out the list
print("Here's your list: ")
index = 1
for item in shopping_list:
print("{}. {}".format(index,item))
index += 1
print("-"*10)
show_help() This line has a red highlight for some reason!!!!
while True: # ask for new items new_item = input("> ")
# be able to quit the app
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() LINE 68!!!!
show_list()