I am self working on an inventory system project, and currently trying to edit key values in my list of dictionaries.
When trying to edit specific key values, a user pointed me in the direction of code that could potentially help me
How to Edit Specific Dictionary Key Values in List of Dictionaries?
I included this in my code and have since updated my code, but now am encountering an error. Here is my relevant code:
import csv
class User_Interaction:
def __init__(self, name):
self.name = name
self.database = DataBase_Management()
self.database.make_dict_items()
self.finder = DB_Fact_Finder(self.database)
def edit_item_interaction(self):
while True:
print("Here is the current inventory:")
self.finder.show_available()
edit_specific_item = self.enter_data("Enter the item # of the item you would like to edit\n", int)
self.finder.does_itemnum_exist(edit_specific_item)
itemnum_found = self.finder.get_item_found()
if itemnum_found:
print("Item Found!")
options = self.enter_data('''What would characteristic would you like to change?? \n1. Price \n2. Quantity \n3. Name\n''', str)
if options == '1':
price = self.enter_data("What is the new price?\n", float )
self.database.change_price(price, edit_specific_item)
print("Price Changed!")
break
and my other class, where the problem lies:
class DataBase_Management(object):
def __init__(self):
self.result = []
def change_price(self, price, item):
new = next((new for new in self.result if new["Price "] == item), None)
new["Price "] = float(price)
self.update_csv()
Upon running this if I try to change the key value of a dictionary whose Item # key value is 10 I get this error:
line 186, in change_price
new["Price "] = float(price)
TypeError: 'NoneType' object does not support item assignment
From what I understand, this occurs when you try to assign something to what is none.
I tried experiment with my variables here to make sure all the assignments were correct and tried this:
def change_price(self, price, item):
print(price)
print(item)
for item in self.result:
print (item)
new = next((new for new in self.result if new["Price "] == item), None)
new["Price "] = float(price)
self.update_csv()
and got the result of
25.99
10
{'Item #': 2, 'Price ': 2.99, 'Quantity': 2, 'Name': 'Muffin2'}
{'Item #': 3, 'Price ': 3.99, 'Quantity': 3, 'Name': 'Cake1'}
{'Item #': 1, 'Price ': 44.99, 'Quantity': 10, 'Name': 'Porcupine'}
{'Item #': 10, 'Price ': 2.99, 'Quantity': 4, 'Name': 'Cookie'}
Type Error....
From what I can tell, my assignments are right and this should work. What is going on here?