简体   繁体   English

TypeError: 'NoneType' object 不支持项目分配,即使变量似乎是正确的

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

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,问题出在哪里:

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:运行此命令时,如果我尝试更改 Item # 键值为 10 的字典的键值,则会收到此错误:

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?这里发生了什么?

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.在某些情况下,您显然期望new获得值None的可能性。

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 "] .将导致您在这些情况下描述的错误,因为您无法为None["Price "]赋值。

By the way, it is unclear what your method def change_price(self, price, item) is supposed to do.顺便说一句,尚不清楚您的方法def change_price(self, price, item)应该做什么。 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?从名称和参数来看,我希望它会为某些给定商品设置价格 - 但您的代码似乎正在寻找具有特定价格匹配item的商品,然后更新该商品的价格? (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) (并且包含一些启动错误,例如不需要生成器,因为无论如何您只能访问第一个元素)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:'NoneType'对象不支持项目分配? - TypeError: 'NoneType' object does not support item assignment? TypeError:“ NoneType”对象不支持项目分配 - TypeError: 'NoneType' object does not support item assignment 如何修复“TypeError: 'NoneType' object 不支持项目分配” - How To Fix “TypeError: 'NoneType' object does not support item assignment” 机械化:TypeError:'NoneType' 对象不支持项目分配 - Mechanize: TypeError: 'NoneType' object does not support item assignment 如何修复 TypeError:'NoneType' 对象不支持项目分配 - how to fix TypeError: 'NoneType' object does not support item assignment Appengine:TypeError:'NoneType'对象不支持项目分配 - Appengine: TypeError: 'NoneType' object does not support item assignment NoneType: TypeError: 'tuple' object 不支持项目分配 - NoneType: TypeError: 'tuple' object does not support item assignment Bottle应用程序的Cork身份验证出现问题:TypeError(“'NoneType'对象不支持项目分配”,) - Trouble with Cork authentication for Bottle app: TypeError(“'NoneType' object does not support item assignment”,) “TypeError: 'NoneType' 对象不支持项目分配” 我该如何解决这个问题? - "TypeError: 'NoneType' object does not support item assignment" How do I fix this problem? 类型错误:“int”对象不支持项目分配 - TypeError: 'int' object does not support item assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM