I am in the process of trying to create a loop, that allows a user to delete an item if the input matches a key value found in a list of dictionaries.
Here is my current list of dicitionaries.
{'Item #': 1, 'Price': 3.99, 'Quantity': 1, 'Name': 'Muffin 1'}
{'Item #': 2, 'Price': 4.99, 'Quantity': 2, 'Name': 'Muffin 2'}
{'Item #': 3, 'Price': 5.99, 'Quantity': 3, 'Name': 'Cookie 1'}
{'Item #': 4, 'Price': 6.99, 'Quantity': 4, 'Name': 'Cookie 2'}
{'Item #': 5, 'Price': 7.99, 'Quantity': 50, 'Name': 'Muffin 3'}
I already have a valid method that deletes the specific dictionary, based upon the value of the item # key.
Here is my relevant code:
def enter_data(self, message, typ):
while True:
try:
v = typ(input(message))
except ValueError:
print(f"Thats not an {typ}!")
continue
else:
break
return v
def delete_item_interaction(self):
while True:
delete_item_number = self.enter_data("Please enter the item # of the item you would like to delete\n", int)
for item item in self.database.result
if item["Item #"] != delete_item_number:
#keep in loop?
#print item not found in any dictionary Item # value in list
else:
#break?
self.database.delete_item(delete_item_number)
print("Item Deleted! Check Inventory again for a refresh!")
I am trying to make a loop, but seem to be struggling with the logic and formatting behind it. I would like something similiar to what I did with enter_data, in which it keeps the user in a input loop, until a valid entry is given. For example, with delete_item_interaction, it should
- In the loop, ask the user for the input of the Item# they would like to delete
- For every dictionary in the list, search if any of them have the input equal to their "Item #" key value
- If found print item found! and exit the loop and send the input to the method that deletes the item
- If not found in any of the dictionaries "Item #" key values, print Item not found, and circle back to the beginning of the loop, to ask for another input.
I understand the logic behind what I am looking for and want, but seem to be having trouble formatting it, and the inclusion of the for loop confuses me. How would I make this method do this?