【发布时间】:2020-09-23 21:24:46
【问题描述】:
我正在尝试为用户编写代码,以便能够删除之前添加的条目。当前列表如下所示:
[{"_Item__id": 1,"item_name": "MAC PowerBook", "item_type": "Computer", "date_add": "01/06/2020", "dom": "16/02/2000", "item_info": "description of product"},
{"_Item__id": 2,"item_name": "Nokia 6210", "item_type": "Phone", "date_add": "01/06/2020", "dom": "01/02/2000", "item_info": "description of product"},
{"_Item__id": 3, "item_name": "Kodak Retina", "item_type": "Camera", "date_add": "04/06/2020", "dom": "20/12/1931", "item_info": "description of product"}]
尝试使用以下代码:
def delete_item():
print("{0:3}\t{1:20}\t{2:10}\t{3:10}".format("ID", "Item", "Date added", "Date manufactured"))
for i in Item.py_collection_list:
print("{0:03d}\t{1:20}\t{2:10}\t{3:10}".format(i.get_id(), i.item_name, i.date_add, i.dom))
remove_item = input('Type name of the item you would like to delete from collection> ')
if remove_item == "item_name":
Item.py_collection_list[{"item_name"}].remove()
然后被重定向到主菜单。
试过这段代码:
def delete_item():
print("{0:3}\t{1:20}\t{2:10}\t{3:10}".format("ID", "Item", "Date added", "Date manufactured"))
for i in Item.py_collection_list:
print("{0:03d}\t{1:20}\t{2:10}\t{3:10}".format(i.get_id(), i.item_name, i.date_add, i.dom))
remove_item = input('Type name of the item you would like to delete from collection> ')
if remove_item == i.item_name:
for item in Item.py_collection_list[:]:
if item['remove_item'] <= 0:
Item.py_collection_list.remove(item)
得到了这个错误:
TypeError: 'Item' object is not subscriptable
试过这段代码:
def delete_item():
print("{0:3}\t{1:20}\t{2:10}\t{3:10}".format("ID", "Item", "Date added", "Date manufactured"))
for i in Item.py_collection_list:
print("{0:03d}\t{1:20}\t{2:10}\t{3:10}".format(i.get_id(), i.item_name, i.date_add, i.dom))
remove_item = input('Type name of the item you would like to delete from collection> ')
if remove_item == i.item_name:
Item.py_collection_list.remove(remove_item)
得到了这个错误:
Item.py_collection_list.remove(remove_item)
ValueError: list.remove(x): x 不在列表中
试过这段代码:
def delete_item():
print("{0:3}\t{1:20}\t{2:10}\t{3:10}".format("ID", "Item", "Date added", "Date manufactured"))
for i in Item.py_collection_list:
print("{0:03d}\t{1:20}\t{2:10}\t{3:10}".format(i.get_id(), i.item_name, i.date_add, i.dom))
remove_item = input('Type name of the item you would like to delete from collection> ')
for i in range(len(Item.py_collection_list)):
if Item.py_collection_list[i]["item_name"] == remove_item:
del Item.py_collection_list[i]
得到了这个错误:
if Item.py_collection_list[i]["item_name"] == remove_item:
TypeError: 'Item' 对象不可下标
试过这段代码:
def delete_item():
print("{0:3}\t{1:20}\t{2:10}\t{3:10}".format("ID", "Item", "Date added", "Date manufactured"))
for i in Item.py_collection_list:
print("{0:03d}\t{1:20}\t{2:10}\t{3:10}".format(i.get_id(), i.item_name, i.date_add, i.dom))
remove_item = input('Type name of the item you would like to delete from collection> ')
if remove_item in Item.py_collection_list:
del Item.py_collection_list[i.item_name]
return True
return False
被重定向到主菜单
这似乎是一个非常直接的创建选项,但我似乎完全在监督这个选项。
【问题讨论】:
-
什么是
Item? “被重定向到主菜单”是什么意思?什么主菜单? -
Item 是一个类,列表是一个变量 - toptal.com/python/… 。上面的代码不会出错,只是让我回到我正在尝试构建的程序的主菜单。
标签: python-3.x list dictionary class-attributes