TypeError: 'NoneType' object does not support item assignment, even though variables seem to be correct

Viewed 26

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?

1 Answers

On this line:

new = next((new for new in self.result if new["Price "] == item), None)

You clearly expect the possibility of new getting the value None in some cases.

So, the next line:

new["Price "] = float(price)

Will cause exactly the error you described in those cases, since you cannot assign a value to None["Price "].

By the way, it is unclear what your method def change_price(self, price, item) is supposed to do. From the name and parameters, I would expect it to set the price on some given item - but your code seems to be looking for an items with a specific price matching item and then updating the price of that? (and contains a few errors to boot, like the lack of a need for the generator, since you only access the first element if any anyway)

Related