-2

How would I be able to check if a user input equals a key in my dictionary? This is what I have so far:

dict_inventory = {
"Blue Large Gloves": {"Manufacturer": "NA", "Order ID": "PHGLOVLG", "Amount in stock": 20, "Par": "2", "Lot Number": "123456", "Lot Expiration": datetime.date(2023, 3, 5)}, 
"BacT Bottles": {"Manufacturer": "Biomerieux", "Order ID": "NA", "Amount in stock": 2, "Par": 3, "Lot Number":"789456", "Lot Expiration": datetime.date(2021, 5, 26)}, 
"pH Buffer 10": {"Manufacturer" : "ThermoScientific", "Order ID": "NA", "Amount in stock": 10, "Par":10, "Lot Number": "35678", "Lot Expiration": datetime.date(2020, 3, 14)}
}

item_taken = input("Which item was taken from stock?: ")
        for k in dict_inventory:
            if item_taken == dict_inventory[k]:
                user_int = int(input(f"How many {item_taken} were taken from stock?: "))
                dict_inventory[item_taken]["Amount in stock"] -= user_int
            else:
                print("Item does not exist")#
coderoftheday
  • 1,987
  • 4
  • 7
  • 21
  • does https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary answer your question – coderoftheday Jan 04 '21 at 00:06
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). Stack Overflow is not intended to replace existing tutorials and documentation. This already exists on Stack Overflow in easily searchable form, and it's included in any tutorial on dicts. – Prune Jan 04 '21 at 00:09

1 Answers1

0
import datetime
dict_inventory = { "Blue Large Gloves": {"Manufacturer": "NA", "Order ID": "PHGLOVLG", "Amount in stock": 20, "Par": "2", "Lot Number": "123456", "Lot Expiration": datetime.date(2023, 3, 5)},
                  "BacT Bottles": {"Manufacturer": "Biomerieux", "Order ID": "NA", "Amount in stock": 2, "Par": 3, "Lot Number":"789456", "Lot Expiration": datetime.date(2021, 5, 26)},
                  "pH Buffer 10": {"Manufacturer" : "ThermoScientific", "Order ID": "NA", "Amount in stock": 10, "Par":10, "Lot Number": "35678", "Lot Expiration": datetime.date(2020, 3, 14)} }

item_taken = input("Which item was taken from stock?: ")
exists=0
for k in dict_inventory:
    if item_taken == k:
        exists=1
if exists==1:
    print('Oh no I loved that.')
    user_int = int(input(f"How many {item_taken} were taken from stock?: "))
    dict_inventory[item_taken]["Amount in stock"] -= user_int
else: print("Item does not exist")
AndrewGraham
  • 310
  • 1
  • 8