I am having trouble with some undefined variables in my code for a school assignment using nested loops and functions. Also, if you happen to spot any other errors please lmk.
Code:
shopping_lists = [
['toothpaste', 'q-tips', 'milk'],
['milk', 'candy', 'apples'],
['planner', 'pencils', 'q-tips']
]
customer_input = ''
#prints shopping lists
print(shopping_lists)
print ('')
print("Press '1' to update an item, '2' to view an item, or '3' to view a list")
customer_input = input("What do you want to do? ")
if customer_input == '1':
def update_list(List, Item, newItem):
list = int(input('What list would you like to update? Answer using 1, 2, or 3. ')-1)
print (shopping_lists[list])
itm = int(input('What item would you like to view? ')-1)
print (shopping_lists[list][itm])
newItm = input('What would you like to change the item to? ')
shopping_lists[list][itm] = newItm
update_list(list, itm, newItm)
def view_item():
pass
def view_list():
pass
#While loop
while 'stop' not in customer_input:
update_list(list, itm, newItm)
I would rearrange your execution flow as follows.
shopping_lists = [
['toothpaste', 'q-tips', 'milk'],
['milk', 'candy', 'apples'],
['planner', 'pencils', 'q-tips']
]
def handle_action(action):
if action == '1':
update_list()
elif action == '2':
view_item()
elif action == '3':
view_list()
else:
pass
# What if an unrecognized action is used?
def update_list():
list_number = int(input('What list would you like to update? Answer using 1, 2, or 3. ')) - 1
print(shopping_lists[list_number])
itm = int(input('What item would you like to view? ')) - 1
print(shopping_lists[list_number][itm])
newItm = input('What would you like to change the item to? ')
shopping_lists[list_number][itm] = newItm
def view_item():
pass
def view_list():
pass
#While loop
customer_input = ''
while customer_input != 'stop':
print(shopping_lists)
print("Press 1 to update an item, 2 to view an item, or 3 to view a list")
customer_input = input("What do you want to do? ")
handle_action(customer_input)
Notice the difference on the usage of stop as a break word for the loop. And the handle_action function to control a switch of what you are doing.
I also renamed list
to list_number
because list
is a type name in python.