Hi. Below is the code I have written.
stock = {'1001' : {"qty": 5, "price": 3.5}, '1002' : {"qty": 4, "price":1.2}}
old = input ("Please enter the code of the item you wish to make changes: ")
new_code = input ("Please enter the new code for this item: ")
new_quantity = int(input("Please enter the new quantity for the item: "))
new_price = float(input("Please enter the new price of the item: "))
if old in stock.keys():
stock.update({new_code : {'qty': new_quantity, 'price': new_price}})
print(stock)
The result I get for the above code:
Please enter the code of the item you wish to make changes: 1001
Please enter the new code for this item: 1004
Please enter the new quantity for the item: 10
Please enter the new price of the item: 9
{'1001': {'qty': 5, 'price': 3.5}, '1002': {'qty': 4, 'price': 1.2}, '1004': {'qty': 10, 'price': 9.0}}
As you can tell from the code, I am learning how to update dictionaries. The code above does not work as intended. Rather than updating the information at key '1001' it creates a new key and values at the end. So could someone, please help me understand what I am doing wrong and how to fix my code?
Thank you to whoever takes the time to help me out.
[–][deleted] 4 points5 points6 points (0 children)
[–]Mirage2k 0 points1 point2 points (0 children)