if main == 'remove':
for count, item in enumerate(grocery_list, 1):
print(f'{count}. {item}')
which_item = input('Which item do you want to remove? Type in the name of the item please! ')
del grocery_list[int(which_item-1)]
print('Your item has been removed! ')
continue
I’m trying to let user remove an item by typing in the enumerated index. When they do type in remove, it give them a list like this:
- item
- item
- item
I tried to do del grocery_list[int(which_item-1)] but that gave an error. I want one subtracted from the which_item variable.
>Solution :
If position of item is asked to the user:
if main == 'remove':
for count, item in enumerate(grocery_list, 1):
print(f'{count}. {item}')
which_item = int(input('Which item do you want to remove? Type in the position of the item please! '))
grocery_list.pop(which_item-1)
print('Your item has been removed! ')
If value of item is asked to the user:
if main == 'remove':
for count, item in enumerate(grocery_list, 1):
print(f'{count}. {item}')
which_item = input('Which item do you want to remove? Type in the name of the item please! ')
grocery_list.remove(which_item)
print('Your item has been removed! ')