我正在尝试为用户编写代码,以便能够删除以前添加的条目。当前列表如下所示:
[{"_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:
类型错误:"项"对象不可下标
尝试了以下代码:
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
已重定向到主菜单
这似乎是一个非常直接的选择,但我似乎完全监督了这个选项。
我建议先单独打印词典,即如果您想向用户提供可用项目列表,然后再要求输入删除项目。
接下来,您可以将输入带到循环之外,如下所示。
remove_item = input("Type name of the item you would like to delete from collection:")
for i in Item.py_collection_list:
if remove_item == i.item_name:
Item.py_collection_list.remove(i)
这将从列表中删除与该项对应的对象。
如果要删除项对象的"item_name"属性,请使用以下命令。
remove_item = input("Type name of the item you would like to delete from collection:")
for i in Item.py_collection_list:
if remove_item == i.item_name:
delattr(i, "item_name")